-
Notifications
You must be signed in to change notification settings - Fork 75
/
STHTTPRequestTests.m
109 lines (69 loc) · 2.92 KB
/
STHTTPRequestTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// STHTTPRequestDemoTests.m
// STHTTPRequestDemoTests
//
// Created by Nicolas Seriot on 8/10/12.
// Copyright (c) 2012 Nicolas Seriot. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "STHTTPRequest.h"
#import "STHTTPRequestTestResponse.h"
#import "STHTTPRequestTestResponseQueue.h"
#import "STHTTPRequest+UnitTests.h"
@interface STHTTPRequestTests : XCTestCase
@end
@implementation STHTTPRequestTests
- (void)setUp {
[super setUp];
// Set-up code here.
[STHTTPRequestTestResponseQueue sharedInstance].responses = [NSMutableArray array];
}
- (void)tearDown {
// Tear-down code here.
NSUInteger numberOfReponsesLeft = [[STHTTPRequestTestResponseQueue sharedInstance].responses count];
XCTAssertTrue(numberOfReponsesLeft == 0, @"still %@ responses in queue", @(numberOfReponsesLeft));
[super tearDown];
}
- (void)testExample {
STHTTPRequestTestResponseQueue *queue = [STHTTPRequestTestResponseQueue sharedInstance];
STHTTPRequestTestResponse *tr = [STHTTPRequestTestResponse testResponseWithBlock:^(STHTTPRequest *r) {
r.responseStatus = 200; // by default
r.responseHeaders = @{ @"key" : @"value" };
r.responseString = @"OK";
// r.error = [NSError errorWithDomain:@"MyDomain" code:0 userInfo:nil]; // to simulate errors
}];
[queue enqueue:tr];
/**/
STHTTPRequest *r = [STHTTPRequest requestWithURLString:@"http://www.google.com"];
r.completionBlock = ^(NSDictionary *headers, NSString *responseString) {
// use response
};
r.errorBlock = ^(NSError *error) {
// use error
};
[r startAsynchronous]; // will actually get executed sychronously for tests
XCTAssertNil(r.error, @"error should be nil: %@", r.error);
XCTAssertTrue(r.responseStatus == 200, @"bad response status: %@", @(r.responseStatus));
XCTAssertEqualObjects(r.responseHeaders, @{ @"key" : @"value" }, @"bad headers: %@", [r responseHeaders]);
XCTAssertEqualObjects(r.responseString, @"OK", @"bad response: %@", r.responseString);
}
- (void)testStreaming {
STHTTPRequest *r = [STHTTPRequest requestWithURLString:@"http://www.google.com"];
r.downloadProgressBlock = ^(NSData *data, int64_t totalBytesReceived, int64_t totalBytesExpectedToReceive) {
NSLog(@"-- %@", data);
};
r.errorBlock = ^(NSError *error) {
// use error
};
[r startAsynchronous]; // will actually get executed sychronously for tests
/**/
[r unitTests_addDownloadProgressUTF8String:@"asd"];
XCTAssertTrue([r.responseData length] == 3, @"");
}
- (void)testStringByAppendingGETParameters {
NSString *s = @"http://www.test.com/x?b=1";
NSDictionary *d = @{@"a":@"1", @"c":@"1"};
NSString *s2 = [s st_stringByAppendingGETParameters:d doApplyURLEncoding:NO];
XCTAssertTrue(s2, @"http://www.test.com/x?b=1&a=1&c=1");
}
@end