| 1 | // |
|---|
| 2 | // HatenaBookmarkTest.m |
|---|
| 3 | // HatebuCoreData |
|---|
| 4 | // |
|---|
| 5 | // Created by mootoh on 5/18/08. |
|---|
| 6 | // Copyright 2008 deadbeaf.org. All rights reserved. |
|---|
| 7 | // |
|---|
| 8 | |
|---|
| 9 | #import "HatenaBookmark.h" |
|---|
| 10 | #import "HatenaBookmarkTest.h" |
|---|
| 11 | #import "AuthPrivate.h" |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | @implementation HatenaBookmarkTest |
|---|
| 15 | -(void) setUp { |
|---|
| 16 | cred_ = WSSE_credentials(AUTH_USER, AUTH_PASS); |
|---|
| 17 | STAssertNotNil(cred_, @"credential", cred_); |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | -(void) testURLs { |
|---|
| 21 | STAssertEquals(@"http://b.hatena.ne.jp/atom", [[Bookmark baseURL] absoluteString], @"base URL check", nil); |
|---|
| 22 | |
|---|
| 23 | // XXX I don't know why this assert failed... |
|---|
| 24 | #if 0 |
|---|
| 25 | STAssertEquals(@"http://b.hatena.ne.jp/atom/edit/XXX", [[Bookmark editURL:@"XXX"] absoluteString], @"edit URL check", nil); |
|---|
| 26 | #endif // 0 |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | -(void) testFeedUriGet { |
|---|
| 30 | NSDictionary *fields = [NSDictionary dictionaryWithObject:cred_ forKey:@"X-WSSE"]; |
|---|
| 31 | NSURLRequest *req = [Bookmark makeRequest:[Bookmark feedURL] withFields:fields]; |
|---|
| 32 | STAssertNotNil(req, @"request create failed"); |
|---|
| 33 | |
|---|
| 34 | NSString *result = [Bookmark sendRequest:req]; |
|---|
| 35 | STAssertNotNil(result, @"sendRequest failed"); |
|---|
| 36 | STAssertTrue([result hasPrefix:@"<?xml version="], @"feed should be xml"); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | -(NSString *)latestBookmark { |
|---|
| 40 | NSDictionary *fields = [NSDictionary dictionaryWithObject:cred_ forKey:@"X-WSSE"]; |
|---|
| 41 | NSURLRequest *req = [Bookmark makeRequest:[Bookmark feedURL] withFields:fields]; |
|---|
| 42 | NSString *recent = [Bookmark sendRequest:req]; |
|---|
| 43 | |
|---|
| 44 | NSXMLDocument *doc = [[NSXMLDocument alloc] initWithXMLString:recent options:NSXMLDocumentTidyXML error:nil]; |
|---|
| 45 | STAssertNotNil(doc, @"parse error", nil); |
|---|
| 46 | |
|---|
| 47 | NSArray *ary = [[doc rootElement] nodesForXPath: @"/feed/entry" error: nil]; |
|---|
| 48 | STAssertNotNil(ary, @"no entries", nil); |
|---|
| 49 | |
|---|
| 50 | NSXMLElement *entry = [ary objectAtIndex:0]; |
|---|
| 51 | STAssertNotNil(entry, @"bad entry", nil); |
|---|
| 52 | |
|---|
| 53 | // extract bookmark eid |
|---|
| 54 | NSString *combinedId = [[[entry elementsForName: @"id"] objectAtIndex:0] stringValue]; |
|---|
| 55 | NSRange lastHyphen = [combinedId rangeOfString:@"-" options:NSBackwardsSearch]; |
|---|
| 56 | NSString *idStr = [combinedId substringFromIndex:lastHyphen.location+1]; |
|---|
| 57 | STAssertNotNil(idStr, @"bad eid", nil); |
|---|
| 58 | return idStr; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | -(void) testLatest { |
|---|
| 62 | NSString *latest = [self latestBookmark]; |
|---|
| 63 | //NSLog(@"latest entry id : %@", latest); |
|---|
| 64 | STAssertNotNil(latest, @"latest", nil); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | -(void) testEditUriGet { |
|---|
| 68 | NSDictionary *fields = [NSDictionary dictionaryWithObject:cred_ forKey:@"X-WSSE"]; |
|---|
| 69 | NSURLRequest *req = [Bookmark makeRequest:[Bookmark editURL:[self latestBookmark]] withFields:fields]; |
|---|
| 70 | STAssertNotNil(req, @"request create failed"); |
|---|
| 71 | |
|---|
| 72 | NSString *result = [Bookmark sendRequest:req]; |
|---|
| 73 | NSLog(@"result : %@", result); |
|---|
| 74 | STAssertNotNil(result, @"sendRequest failed"); |
|---|
| 75 | STAssertTrue([result hasPrefix:@"<?xml version="], @"feed should be xml"); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | -(void) testEditUriPost { |
|---|
| 79 | NSDictionary *fields = [NSDictionary dictionaryWithObject:cred_ forKey:@"X-WSSE"]; |
|---|
| 80 | NSMutableURLRequest *req = [Bookmark makeRequest:[Bookmark editURL:[self latestBookmark]] withFields:fields]; |
|---|
| 81 | STAssertNotNil(req, @"request create failed"); |
|---|
| 82 | |
|---|
| 83 | NSString *entryFeed = [Bookmark sendRequest:req]; |
|---|
| 84 | NSLog(@"entryFeed : %@", entryFeed); |
|---|
| 85 | STAssertNotNil(entryFeed, @"sendRequest failed"); |
|---|
| 86 | STAssertTrue([entryFeed hasPrefix:@"<?xml version="], @"feed should be xml"); |
|---|
| 87 | |
|---|
| 88 | // parse entry |
|---|
| 89 | NSXMLDocument *doc = [[NSXMLDocument alloc] initWithXMLString:entryFeed options:NSXMLDocumentTidyXML error:nil]; |
|---|
| 90 | STAssertNotNil(doc, @"parse error", nil); |
|---|
| 91 | |
|---|
| 92 | NSXMLElement *oldTitleElm = [[[doc rootElement] nodesForXPath: @"/entry/title" error: nil] objectAtIndex:0]; |
|---|
| 93 | STAssertNotNil(oldTitleElm, @"bad title", nil); |
|---|
| 94 | NSString *oldTitle = [oldTitleElm stringValue]; |
|---|
| 95 | |
|---|
| 96 | NSXMLElement *oldSummaryElm = [[[doc rootElement] nodesForXPath: @"/entry/summary" error: nil] objectAtIndex:0]; |
|---|
| 97 | NSString *oldSummary = [oldSummaryElm stringValue]; |
|---|
| 98 | STAssertNotNil(oldSummary, @"bad title", nil); |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | NSString *suffix = @" testEditUriPost"; |
|---|
| 102 | NSString *newTitle = [NSString stringWithFormat:@"%@ %@", oldTitle, suffix]; |
|---|
| 103 | NSString *newSummary = [NSString stringWithFormat:@"%@ %@", oldSummary, suffix]; |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | // include tags in summary |
|---|
| 107 | NSArray *tags = [[doc rootElement] elementsForName: @"dc:subject"]; |
|---|
| 108 | for (NSXMLElement *t in tags) { |
|---|
| 109 | NSString *name = [t stringValue]; |
|---|
| 110 | newSummary = [NSString stringWithFormat:@"[%@]%@", name, newSummary]; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | newSummary = [NSString stringWithFormat:@"[HatebuCoreData]%@", newSummary]; |
|---|
| 114 | |
|---|
| 115 | [oldTitleElm setStringValue:newTitle]; |
|---|
| 116 | [oldSummaryElm setStringValue:newSummary]; |
|---|
| 117 | |
|---|
| 118 | // PUT it |
|---|
| 119 | [req setHTTPMethod:@"PUT"]; |
|---|
| 120 | [req setHTTPBody:[doc XMLData]]; |
|---|
| 121 | |
|---|
| 122 | NSString *result = [Bookmark sendRequest:req]; |
|---|
| 123 | STAssertNotNil(result, @"sendRequest failed"); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | @end |
|---|
| 127 | // vim:set ft=objc: |
|---|