ObjFW  Diff

Differences From Artifact [8ae0f89264]:

To Artifact [d3dab1d884]:


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
128
129
130
131








132
133
134























135
136
137
138
139
140




























141



142
143
144
145
146



























147


148













149
150
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155





156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200







-
+
+

-
+
-
-
-
+
+
+
-




-
+


-

-
-
-
-
-
-
-
+
-
-
+
-
-
-
-
-
+
-
-

-
-
+
-
-
-
-
-
-
+
-
-
-
-
+
-
-
+
-
-

-
-
-
-
-
-
-
+
-

-

-












-
+

+
-
+




-
+

-


-

+


+
+
-
-
+
+

-
-


-
-
+
+



-
+

-
-
-
-
-
-
-
+
+
+
+
+
+
+
+



+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+


 */

#include "config.h"

#include <inttypes.h>
#include <string.h>

#import "TestsAppDelegate.h"
#import "ObjFW.h"
#import "ObjFWTest.h"

static OFString *const module = @"OFHTTPClient";
@interface OFHTTPClientTests: OTTestCase <OFHTTPClientDelegate>
static OFCondition *condition;
static OFHTTPResponse *response = nil;

{
	OFHTTPResponse *_response;
}
@interface TestsAppDelegate (HTTPClientTests) <OFHTTPClientDelegate>
@end

@interface HTTPClientTestsServer: OFThread
{
@public
	OFCondition *_condition;
	uint16_t _port;
}
@end

@implementation HTTPClientTestsServer
- (id)main
{
	OFTCPSocket *listener, *client;
	OFSocketAddress address;
	char buffer[5];

@property (readonly, nonatomic) OFCondition *condition;
	[condition lock];

@property (readonly) uint16_t port;
	listener = [OFTCPSocket socket];
	address = [listener bindToHost: @"127.0.0.1" port: 0];
	_port = OFSocketAddressIPPort(&address);
	[listener listen];

@end
	[condition signal];
	[condition unlock];

	client = [listener accept];

@implementation OFHTTPClientTests
	OFEnsure([[client readLine] isEqual: @"GET /foo HTTP/1.1"]);
	OFEnsure([[client readLine] hasPrefix: @"User-Agent:"]);
	OFEnsure([[client readLine] isEqual: @"Content-Length: 5"]);
	OFEnsure([[client readLine] isEqual:
	    @"Content-Type: application/x-www-form-urlencoded; charset=UTF-8"]);

- (void)dealloc
	if (![[client readLine] isEqual:
	    [OFString stringWithFormat: @"Host: 127.0.0.1:%" @PRIu16, _port]])
		OFEnsure(0);

{
	OFEnsure([[client readLine] isEqual: @""]);

	[_response release];
	[client readIntoBuffer: buffer exactLength: 5];
	OFEnsure(memcmp(buffer, "Hello", 5) == 0);

	[client writeString: @"HTTP/1.0 200 OK\r\n"
			     @"cONTeNT-lENgTH: 7\r\n"
			     @"\r\n"
			     @"foo\n"
			     @"bar"];
	[client close];

	[super dealloc];
	return nil;
}
@end

@implementation TestsAppDelegate (OFHTTPClientTests)
-     (void)client: (OFHTTPClient *)client
  wantsRequestBody: (OFStream *)body
	   request: (OFHTTPRequest *)request
{
	[body writeString: @"Hello"];
}

-      (void)client: (OFHTTPClient *)client
  didPerformRequest: (OFHTTPRequest *)request
	   response: (OFHTTPResponse *)response_
	  exception: (id)exception
{
	OFEnsure(exception == nil);
	OTAssertNil(exception);

	[_response release];
	response = [response_ retain];
	_response = [response_ retain];

	[[OFRunLoop mainRunLoop] stop];
}

- (void)HTTPClientTests
- (void)testClient
{
	void *pool = objc_autoreleasePoolPush();
	HTTPClientTestsServer *server;
	OFIRI *IRI;
	OFHTTPClient *client;
	OFHTTPRequest *request;
	OFHTTPClient *client;
	OFData *data;

	server = [[[HTTPClientTestsServer alloc] init] autorelease];
	server.supportsSockets = true;
	condition = [OFCondition condition];
	[condition lock];

	[server.condition lock];

	server = [[[HTTPClientTestsServer alloc] init] autorelease];
	server.supportsSockets = true;
	[server start];

	[condition wait];
	[condition unlock];
	[server.condition wait];
	[server.condition unlock];

	IRI = [OFIRI IRIWithString:
	    [OFString stringWithFormat: @"http://127.0.0.1:%" @PRIu16 "/foo",
					server->_port]];
					server.port]];

	TEST(@"-[asyncPerformRequest:]",
	    (client = [OFHTTPClient client]) && (client.delegate = self) &&
	    (request = [OFHTTPRequest requestWithIRI: IRI]) &&
	    (request.headers =
	    [OFDictionary dictionaryWithObject: @"5"
					forKey: @"Content-Length"]) &&
	    R([client asyncPerformRequest: request]))
	request = [OFHTTPRequest requestWithIRI: IRI];
	request.headers = [OFDictionary
	    dictionaryWithObject: @"5"
			  forKey: @"Content-Length"];

	client = [OFHTTPClient client];
	client.delegate = self;
	[client asyncPerformRequest: request];

	[[OFRunLoop mainRunLoop] runUntilDate:
	    [OFDate dateWithTimeIntervalSinceNow: 2]];

	OTAssertNotNil(_response);
	OTAssertNotNil([_response.headers objectForKey: @"Content-Length"]);

	data = [_response readDataUntilEndOfStream];
	OTAssertEqual(data.count, 7);
	OTAssertEqual(data.itemSize, 1);
	OTAssertEqual(memcmp(data.items, "foo\nbar", 7), 0);

	OTAssertNil([server join]);
}
@end

@implementation HTTPClientTestsServer
@synthesize condition = _condition, port = _port;

- (instancetype)init
{
	self = [super init];

	@try {
		_condition = [[OFCondition alloc] init];
	} @catch (id e) {
	[response autorelease];

	TEST(@"Asynchronous handling of requests", response != nil)

	TEST(@"Normalization of server header keys",
	    [response.headers objectForKey: @"Content-Length"] != nil)
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_condition release];

	[super dealloc];
}

- (id)main
{
	OFTCPSocket *listener, *client;
	OFSocketAddress address;
	bool sawHost = false, sawContentLength = false, sawContentType = false;
	bool sawUserAgent = false;
	char buffer[5];

	[_condition lock];

	listener = [OFTCPSocket socket];
	address = [listener bindToHost: @"127.0.0.1" port: 0];
	_port = OFSocketAddressIPPort(&address);
	[listener listen];

	[_condition signal];
	[_condition unlock];
	client = [listener accept];
	TEST(@"Correct parsing of data",
	    (data = [response readDataUntilEndOfStream]) &&
	    data.count == 7 && memcmp(data.items, "foo\nbar", 7) == 0)

	[server join];

	if (![[client readLine] isEqual: @"GET /foo HTTP/1.1"])
		return @"Wrong request";

	for (size_t i = 0; i < 4; i++) {
		OFString *line = [client readLine];

		if ([line isEqual: [OFString stringWithFormat:
		    @"Host: 127.0.0.1:%" @PRIu16, _port]])
			sawHost = true;
		else if ([line isEqual: @"Content-Length: 5"])
			sawContentLength = true;
		if ([line isEqual: @"Content-Type: application/"
		    @"x-www-form-urlencoded; charset=UTF-8"])
			sawContentType = true;
		else if ([line hasPrefix: @"User-Agent:"])
			sawUserAgent = true;
	}

	if (!sawHost)
		return @"Missing host";
	if (!sawContentLength)
		return @"Missing content length";
	if (!sawContentType)
		return @"Missing content type";
	if (!sawUserAgent)
		return @"Missing user agent";

	if (![[client readLine] isEqual: @""])
		return @"Missing empty line";
	objc_autoreleasePoolPop(pool);

	[client readIntoBuffer: buffer exactLength: 5];
	if (memcmp(buffer, "Hello", 5) != 0)
		return @"Missing body";

	[client writeString: @"HTTP/1.0 200 OK\r\n"
			     @"cONTeNT-lENgTH: 7\r\n"
			     @"\r\n"
			     @"foo\n"
			     @"bar"];
	[client close];

	return nil;
}
@end