ShaarliMTest.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // ShaarliMTest.m
  3. // ShaarliOS
  4. //
  5. // Created by Marcus Rohrmoser on 19.07.15.
  6. // Copyright (c) 2015-2016 Marcus Rohrmoser http://mro.name/me. All rights reserved.
  7. //
  8. // This program is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. #import <UIKit/UIKit.h>
  22. #import "XCTestCase+Tools.h"
  23. #import "ShaarliM.h"
  24. #define M_FORM @"form"
  25. #define F_TOKEN F_K_TOKEN
  26. #define M_HAS_LOGOUT @"has_logout"
  27. NSDictionary *parseShaarliHtml(NSData *data, NSError **error);
  28. @interface ShaarliM() <NSURLSessionDataDelegate>
  29. @property (strong, nonatomic) NSURL *endpointURL;
  30. @property (strong, nonatomic) NSString *userName;
  31. @property (strong, nonatomic) NSString *passWord;
  32. @property (assign, nonatomic) BOOL privateDefault;
  33. @end
  34. @interface ShaarliMTest : XCTestCase
  35. @end
  36. @implementation ShaarliMTest
  37. -(void)setUp
  38. {
  39. [super setUp];
  40. // Put setup code here. This method is called before the invocation of each test method in the class.
  41. }
  42. -(void)tearDown
  43. {
  44. // Put teardown code here. This method is called after the invocation of each test method in the class.
  45. [super tearDown];
  46. }
  47. #if 0
  48. -(void)_testPostData
  49. {
  50. NSDictionary *d = @ {
  51. @"key0" : @"val ue",
  52. @"key1" : @"val?ue",
  53. @"key2" : @"val&ue",
  54. };
  55. XCTAssertEqualObjects(@"key2=val%26ue&key1=val%3Fue&key0=val%20ue", [[NSString alloc] initWithData:[d postData] encoding:NSUTF8StringEncoding], @"");
  56. }
  57. #endif
  58. -(void)testStringByStrippingTags
  59. {
  60. NSMutableArray *a = [NSMutableArray arrayWithCapacity:10];
  61. {
  62. [a removeAllObjects];
  63. XCTAssertEqualObjects(@"", [@"" stringByStrippingTags:a], @"");
  64. XCTAssertEqual(0, a.count, @"");
  65. }
  66. {
  67. [a removeAllObjects];
  68. XCTAssertEqualObjects(@" \n ", [@" \n " stringByStrippingTags:a], @"");
  69. XCTAssertEqual(0, a.count, @"");
  70. }
  71. {
  72. [a removeAllObjects];
  73. XCTAssertEqualObjects(@"", [@" #ShaarliOS " stringByStrippingTags:a], @"");
  74. XCTAssertEqual(1, a.count, @"");
  75. XCTAssertEqualObjects(@"ShaarliOS", a[0], @"");
  76. }
  77. {
  78. [a removeAllObjects];
  79. XCTAssertEqualObjects(@"foo", [@"#Shaarli💫 #b #c ##d \nfoo" stringByStrippingTags:a], @"");
  80. XCTAssertEqual(4, a.count, @"");
  81. XCTAssertEqualObjects(@"Shaarli💫", a[0], @"");
  82. XCTAssertEqualObjects(@"b", a[1], @"");
  83. XCTAssertEqualObjects(@"c", a[2], @"");
  84. XCTAssertEqualObjects(@"#d", a[3], @"");
  85. }
  86. }
  87. #if 0
  88. -(void)testHttpGetParams
  89. {
  90. NSURL *url = [NSURL URLWithString:@"http://links.mro.name/?post=http%3A%2F%2Fww.heise.de%2Fa&title=Ti+tle&description=Des%20crip%20tio=n&source=http%3A%2F%2Fapp.mro.name%2FShaarliOS"];
  91. NSDictionary *p = [url dictionaryWithHttpFormUrl];
  92. XCTAssertEqual(4, p.count, @"");
  93. XCTAssertEqualObjects(@"http://ww.heise.de/a", p[K_F_POST], @"");
  94. XCTAssertEqualObjects(@"Ti tle", p[K_F_TITLE], @"");
  95. XCTAssertEqualObjects(@"Des crip tio=n", p[K_F_DESCRIPTION], @"");
  96. XCTAssertEqualObjects(@"http://app.mro.name/ShaarliOS", p[K_F_SOURCE], @"");
  97. XCTAssertEqualObjects(@"Des%20crip%20tion", [@"Des crip tion" stringByAddingPercentEscapesForHttpFormUrl], @"hu");
  98. p = @ {
  99. @"descr=iption" : @"Des crip tion"
  100. };
  101. XCTAssertEqualObjects(@"descr%3Diption=Des%20crip%20tion", [p stringByAddingPercentEscapesForHttpFormUrl], @"hu");
  102. }
  103. -(void)testParseShaarliHtml
  104. {
  105. {
  106. NSDictionary *ret = parseShaarliHtml([self dataWithContentsOfFixture:@"testLogin.ok" withExtension:@"html"], nil);
  107. // MRLogD(@"%@", ret, nil);
  108. XCTAssertEqual(4, ret.count, @"entries' count");
  109. XCTAssertEqualObjects(@ (1), ret[M_HAS_LOGOUT], @"");
  110. XCTAssertEqualObjects(@"links.mro", ret[K_F_TITLE], @"");
  111. XCTAssertNotNil(ret[@"headerform"], @"");
  112. XCTAssertEqual(5, [ret[M_FORM] count], @"entries' count");
  113. XCTAssertEqualObjects(@"20150715_200440", ret[M_FORM][@"edit_link"], @"");
  114. XCTAssertEqualObjects(@"20150715_200440", ret[M_FORM][@"lf_linkdate"], @"");
  115. XCTAssertEqualObjects(@"", ret[M_FORM][@"searchtags"], @"");
  116. XCTAssertEqualObjects(@"", ret[M_FORM][@"searchterm"], @"");
  117. XCTAssertEqualObjects(@"6ff77552e09da9ef31e0e9d0b717da8933f68975", ret[M_FORM][F_K_TOKEN], @"");
  118. }
  119. {
  120. NSDictionary *ret = parseShaarliHtml([self dataWithContentsOfFixture:@"testLogin.0" withExtension:@"html"], nil);
  121. // MRLogD(@"%@", ret, nil);
  122. XCTAssertEqual(3, ret.count, @"entries' count");
  123. XCTAssertEqualObjects(@"links.mro", ret[K_F_TITLE], @"");
  124. XCTAssertNotNil(ret[@"headerform"], @"");
  125. XCTAssertEqual(2, [ret[M_FORM] count], @"entries' count");
  126. XCTAssertEqualObjects(@"http://links.mro.name/", ret[M_FORM][F_K_RETURNURL], @"");
  127. XCTAssertEqualObjects(@"20119241badf78a3dcfa55ae58eab429a5d24bad", ret[M_FORM][F_K_TOKEN], @"");
  128. }
  129. {
  130. NSDictionary *ret = parseShaarliHtml([self dataWithContentsOfFixture:@"05.addlink-1" withExtension:@"html"], nil);
  131. // MRLogD(@"%@", ret, nil);
  132. XCTAssertEqual(4, ret.count, @"entries' count");
  133. XCTAssertEqualObjects(@"links.mro", ret[K_F_TITLE], @"");
  134. XCTAssertEqualObjects(@ (YES), ret[M_HAS_LOGOUT], @"");
  135. XCTAssertNotNil(ret[@"headerform"], @"");
  136. XCTAssertEqual(0, [ret[M_FORM] count], @"entries' count");
  137. }
  138. {
  139. NSDictionary *ret = parseShaarliHtml([self dataWithContentsOfFixture:@"05.addlink-2" withExtension:@"html"], nil);
  140. // MRLogD(@"%@", ret, nil);
  141. XCTAssertEqual(3, ret.count, @"entries' count");
  142. XCTAssertEqualObjects(@"links.mro", ret[K_F_TITLE], @"");
  143. XCTAssertEqualObjects(@ (YES), ret[M_HAS_LOGOUT], @"");
  144. XCTAssertEqual(8, [ret[M_FORM] count], @"entries' count");
  145. XCTAssertEqualObjects(@"Cancel", ret[M_FORM][@"cancel_edit"], @"");
  146. XCTAssertEqualObjects(@"20150719_173950", ret[M_FORM][@"lf_linkdate"], @"");
  147. XCTAssertEqualObjects(@"", ret[M_FORM][K_F_LF_TAGS], @"");
  148. XCTAssertEqualObjects(@"Note: ", ret[M_FORM][K_F_LF_TITLE], @"");
  149. XCTAssertEqualObjects(@"?tgI8rw", ret[M_FORM][K_F_LF_URL], @"");
  150. XCTAssertEqualObjects(@"http://links.mro.name/?do=login&post=", ret[M_FORM][F_K_RETURNURL], @"");
  151. XCTAssertEqualObjects(@"Save", ret[M_FORM][@"save_edit"], @"");
  152. XCTAssertEqualObjects(@"e90b4ab4846c221880872003ba47859183da4e6e", ret[M_FORM][F_K_TOKEN], @"");
  153. }
  154. {
  155. NSDictionary *ret = parseShaarliHtml([self dataWithContentsOfFixture:@"banned" withExtension:@"html"], nil);
  156. // MRLogD(@"%@", ret, nil);
  157. XCTAssertEqual(2, ret.count, @"entries' count");
  158. XCTAssertEqualObjects(@"links.mro", ret[K_F_TITLE], @"");
  159. XCTAssertEqualObjects(@"You have been banned from login after too many failed attempts. Try later.", ret[@"headerform"], @"");
  160. XCTAssertEqual(0, [ret[M_FORM] count], @"entries' count");
  161. }
  162. }
  163. -(void)testParseHtmlTags
  164. {
  165. NSDictionary *ret = parseShaarliHtml([self dataWithContentsOfFixture:@"03.tagcloud" withExtension:@"html"], nil);
  166. // MRLogD(@"%@", ret, nil);
  167. XCTAssertEqual(2, ret.count, @"entries' count");
  168. XCTAssertEqualObjects(@"links.mro", ret[K_F_TITLE], @"");
  169. NSArray *tags = ret[@"tags"];
  170. MRLogD(@"%@", tags, nil);
  171. XCTAssertEqual(1794, tags.count, @"");
  172. {
  173. NSArray *sorted = [tags sortedArrayUsingComparator:^NSComparisonResult (NSDictionary * t1, NSDictionary * t2) {
  174. const NSComparisonResult r0 = [t2[@"count"] compare:t1[@"count"]];
  175. if( r0 != NSOrderedSame )
  176. return r0;
  177. return [t1[@"label"] compare:t2[@"label"] options:0];
  178. }
  179. ];
  180. XCTAssertEqualObjects (@"Software", [sorted firstObject][@"label"], @"");
  181. XCTAssertEqualObjects (@ (170), [sorted firstObject][@"count"], @"");
  182. XCTAssertEqualObjects (@"§99StGB", [sorted lastObject][@"label"], @"");
  183. XCTAssertEqualObjects (@ (1), [sorted lastObject][@"count"], @"");
  184. }
  185. }
  186. -(void)testGithubIssue5
  187. {
  188. {
  189. NSDictionary *ret = parseShaarliHtml([self dataWithContentsOfFixture:@"github-issue#5" withExtension:@"html"], nil);
  190. XCTAssertNotNil(ret);
  191. XCTAssertEqual(0, ret.count, @"May be empty, but mustn't crash", nil);
  192. }
  193. {
  194. NSDictionary *ret = parseShaarliHtml([NSData data], nil);
  195. XCTAssertNotNil(ret);
  196. XCTAssertEqual(0, ret.count, @"May be empty, but mustn't crash", nil);
  197. }
  198. {
  199. NSDictionary *ret = parseShaarliHtml(nil, nil);
  200. XCTAssertNotNil(ret);
  201. XCTAssertEqual(0, ret.count, @"May be empty, but mustn't crash", nil);
  202. }
  203. }
  204. #endif
  205. @end