-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathKMSTestCase.m
127 lines (103 loc) · 2.67 KB
/
KMSTestCase.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// Created by Sam Deane on 06/11/2012.
// Copyright 2012 Karelia Software. All rights reserved.
//
#import "KMSTestCase.h"
#import "KMSServer.h"
#import "KMSRegExResponder.h"
#import "KMSResponseCollection.h"
@implementation KMSTestCase
- (void)dealloc
{
[_password release];
[_responses release];
[_server release];
[_url release];
[_user release];
[super dealloc];
}
- (void)tearDown
{
[self cleanupServer];
}
- (BOOL)setupServerWithResponseFileNamed:(NSString *)responsesFile
{
self.user = @"user";
self.password = @"pass";
NSURL* url = [[NSBundle bundleForClass:[self class]] URLForResource:responsesFile withExtension:@"json"];
if (url)
{
self.responses = [KMSResponseCollection collectionWithURL:url];
KMSRegExResponder* responder = [self.responses responderWithName:@"default"];
if (responder)
{
self.server = [KMSServer serverWithPort:0 responder:responder];
XCTAssertNotNil(self.server, @"got server");
if (self.server)
{
[self.server start];
BOOL started = self.server.running;
XCTAssertTrue(started, @"server started ok");
self.url = [NSURL URLWithString:[NSString stringWithFormat:@"%@://127.0.0.1:%ld", self.responses.scheme, (unsigned long)self.server.port]];
}
}
}
else
{
KMSLog(@"no response file called '%@'", responsesFile);
}
return self.server != nil;
}
- (void)cleanupServer
{
[self.server stop];
self.user = nil;
self.password = nil;
self.url = nil;
self.responses = nil;
self.server = nil;
}
- (void)useResponseSet:(NSString*)name
{
KMSResponder* responder = [self.responses responderWithName:name];
if (responder)
{
self.server.responder = responder;
}
}
- (NSURL*)URLForPath:(NSString*)path
{
NSURL* url = [self.url URLByAppendingPathComponent:path];
return url;
}
- (NSString*)stringForRequest:(NSURLRequest*)request
{
__block NSString* string = nil;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse* response, NSData* data, NSError* error)
{
if (error)
{
NSLog(@"got error %@", error);
}
else
{
string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
[self pause];
}];
[self runUntilPaused];
return [string autorelease];
}
- (void)runUntilPaused
{
[self.server runUntilPaused];
}
- (void)pause
{
[self.server pause];
}
- (void)resume
{
[self.server resume];
}
@end