ObjFW  Check-in [851f6138bf]

Overview
Comment:Migrate OFHTTPClientTests to ObjFWTest
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | objfwtest
Files: files | file ages | folders
SHA3-256: 851f6138bf97bf38d6a85208fd94610fc222d4655fc0f07e71b4af54bc1ed1ad
User & Date: js on 2024-02-18 10:11:41
Other Links: branch diff | manifest | tags
Context
2024-02-18
11:08
Migrate OFKernelEventObserverTests to ObjFWTest check-in: aa56dd6d2a user: js tags: objfwtest
10:11
Migrate OFHTTPClientTests to ObjFWTest check-in: 851f6138bf user: js tags: objfwtest
2024-02-17
22:23
Migrate OFHTTPCookieManagerTests to ObjFWTest check-in: 2aad218748 user: js tags: objfwtest
Changes

Modified new_tests/Makefile from [87e80c1e61] to [2da97fd19f].

52
53
54
55
56
57
58

59

60
61
62
63
64
65
66
52
53
54
55
56
57
58
59

60
61
62
63
64
65
66
67







+
-
+







       ${USE_SRCS_SOCKETS}		\
       ${USE_SRCS_SUBPROCESSES}		\
       ${USE_SRCS_THREADS}		\
       ${USE_SRCS_WINDOWS}		\
       testfile_bin.m			\
       testfile_ini.m
SRCS_PLUGINS = OFPluginTests.m
SRCS_SOCKETS = ${OF_HTTP_CLIENT_TESTS_M}	\
SRCS_SOCKETS = OFHTTPCookieManagerTests.m	\
	       OFHTTPCookieManagerTests.m	\
	       OFHTTPCookieTests.m		\
	       OFDNSResolverTests.m		\
	       OFSocketTests.m			\
	       OFTCPSocketTests.m		\
	       OFUDPSocketTests.m		\
	       ${USE_SRCS_IPX}			\
	       ${USE_SRCS_UNIX_SOCKETS}		\

Renamed and modified tests/OFHTTPClientTests.m [8ae0f89264] to new_tests/OFHTTPClientTests.m [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

Modified tests/Makefile from [4c633051dc] to [9ffa7ba9ba].

20
21
22
23
24
25
26
27
28

29
30
31
32
33
34
35
20
21
22
23
24
25
26


27
28
29
30
31
32
33
34







-
-
+







       OFStreamTests.m			\
       OFValueTests.m			\
       OFXMLNodeTests.m			\
       OFXMLParserTests.m		\
       TestsAppDelegate.m		\
       ${USE_SRCS_FILES}		\
       ${USE_SRCS_SOCKETS}
SRCS_SOCKETS = ${OF_HTTP_CLIENT_TESTS_M}	\
	       OFKernelEventObserverTests.m
SRCS_SOCKETS = OFKernelEventObserverTests.m

IOS_USER ?= mobile
IOS_TMP ?= /tmp/objfw-test

include ../buildsys.mk

.PHONY: run run-on-ios run-on-android

Modified tests/TestsAppDelegate.h from [33a2e645a0] to [32ce471abe].

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
63
64
65
66
67
68
69




70
71
72
73
74
75
76







-
-
-
-







- (void)dataTests;
@end

@interface TestsAppDelegate (OFDictionaryTests)
- (void)dictionaryTests;
@end

@interface TestsAppDelegate (OFHTTPClientTests)
- (void)HTTPClientTests;
@end

@interface TestsAppDelegate (OFKernelEventObserverTests)
- (void)kernelEventObserverTests;
@end

@interface TestsAppDelegate (OFListTests)
- (void)listTests;
@end

Modified tests/TestsAppDelegate.m from [fbab5e01e9] to [bc05260f0b].

375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
375
376
377
378
379
380
381



382
383
384
385
386
387
388







-
-
-







	[self listTests];
	[self valueTests];
	[self streamTests];
	[self memoryStreamTests];
#ifdef OF_HAVE_SOCKETS
	[self kernelEventObserverTests];
#endif
#if defined(OF_HAVE_SOCKETS) && defined(OF_HAVE_THREADS)
	[self HTTPClientTests];
#endif
	[self XMLParserTests];
	[self XMLNodeTests];

	[OFStdOut reset];

#if defined(OF_IOS)
	[OFStdOut writeFormat: @"%d tests failed!", _fails];