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
|
/*
* Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im>
*
* All rights reserved.
*
* This file is part of ObjFW. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE.QPL included in
* the packaging of this file.
*
* Alternatively, it may be distributed under the terms of the GNU General
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#include <inttypes.h>
#include <string.h>
#import "TestsAppDelegate.h"
static OFString *module = @"OFHTTPClient";
static OFCondition *cond;
static OFHTTPResponse *response = nil;
@interface TestsAppDelegate (HTTPClientTests) <OFHTTPClientDelegate>
@end
@interface HTTPClientTestsServer: OFThread
{
@public
uint16_t _port;
}
@end
@implementation HTTPClientTestsServer
- (id)main
{
OFTCPSocket *listener, *client;
char buffer[5];
[cond lock];
listener = [OFTCPSocket socket];
_port = [listener bindToHost: @"127.0.0.1" port: 0];
[listener listen];
[cond signal];
[cond unlock];
client = [listener accept];
if (![[client readLine] isEqual: @"GET /foo HTTP/1.1"])
OFEnsure(0);
if (![[client readLine] hasPrefix: @"User-Agent:"])
OFEnsure(0);
if (![[client readLine] isEqual: @"Content-Length: 5"])
OFEnsure(0);
if (![[client readLine] isEqual:
@"Content-Type: application/x-www-form-urlencoded; charset=UTF-8"])
OFEnsure(0);
if (![[client readLine] isEqual:
[OFString stringWithFormat: @"Host: 127.0.0.1:%" @PRIu16, _port]])
OFEnsure(0);
if (![[client readLine] isEqual: @""])
OFEnsure(0);
[client readIntoBuffer: buffer exactLength: 5];
if (memcmp(buffer, "Hello", 5) != 0)
OFEnsure(0);
[client writeString: @"HTTP/1.0 200 OK\r\n"
@"cONTeNT-lENgTH: 7\r\n"
@"\r\n"
@"foo\n"
@"bar"];
[client close];
|
|
|
|
|
|
|
|
<
<
|
<
<
|
<
<
|
|
<
|
<
|
<
|
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
|
/*
* Copyright (c) 2008-2022 Jonathan Schleifer <js@nil.im>
*
* All rights reserved.
*
* This file is part of ObjFW. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE.QPL included in
* the packaging of this file.
*
* Alternatively, it may be distributed under the terms of the GNU General
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#include <inttypes.h>
#include <string.h>
#import "TestsAppDelegate.h"
static OFString *const module = @"OFHTTPClient";
static OFCondition *condition;
static OFHTTPResponse *response = nil;
@interface TestsAppDelegate (HTTPClientTests) <OFHTTPClientDelegate>
@end
@interface HTTPClientTestsServer: OFThread
{
@public
uint16_t _port;
}
@end
@implementation HTTPClientTestsServer
- (id)main
{
OFTCPSocket *listener, *client;
char buffer[5];
[condition lock];
listener = [OFTCPSocket socket];
_port = [listener bindToHost: @"127.0.0.1" port: 0];
[listener listen];
[condition signal];
[condition unlock];
client = [listener accept];
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"]);
if (![[client readLine] isEqual:
[OFString stringWithFormat: @"Host: 127.0.0.1:%" @PRIu16, _port]])
OFEnsure(0);
OFEnsure([[client readLine] isEqual: @""]);
[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];
|
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
void *pool = objc_autoreleasePoolPush();
HTTPClientTestsServer *server;
OFURL *URL;
OFHTTPClient *client;
OFHTTPRequest *request;
OFData *data;
cond = [OFCondition condition];
[cond lock];
server = [[[HTTPClientTestsServer alloc] init] autorelease];
server.supportsSockets = true;
[server start];
[cond wait];
[cond unlock];
URL = [OFURL URLWithString:
[OFString stringWithFormat: @"http://127.0.0.1:%" @PRIu16 "/foo",
server->_port]];
TEST(@"-[asyncPerformRequest:]",
(client = [OFHTTPClient client]) && (client.delegate = self) &&
|
|
|
|
|
|
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
void *pool = objc_autoreleasePoolPush();
HTTPClientTestsServer *server;
OFURL *URL;
OFHTTPClient *client;
OFHTTPRequest *request;
OFData *data;
condition = [OFCondition condition];
[condition lock];
server = [[[HTTPClientTestsServer alloc] init] autorelease];
server.supportsSockets = true;
[server start];
[condition wait];
[condition unlock];
URL = [OFURL URLWithString:
[OFString stringWithFormat: @"http://127.0.0.1:%" @PRIu16 "/foo",
server->_port]];
TEST(@"-[asyncPerformRequest:]",
(client = [OFHTTPClient client]) && (client.delegate = self) &&
|