@@ -4,36 +4,28 @@ pragma solidity ^0.8.13;
4
4
import "forge-std/Test.sol " ;
5
5
6
6
import {HTTP} from "@src/HTTP.sol " ;
7
- import {HTTPBuilder} from "@src/HTTPBuilder.sol " ;
8
- import {strings} from "solidity-stringutils/strings.sol " ;
9
- import {StringMap} from "@src/StringMap.sol " ;
10
7
import {strings} from "solidity-stringutils/strings.sol " ;
11
8
import {stdJson} from "forge-std/StdJson.sol " ;
12
9
13
10
contract HTTPTest is Test {
11
+ using HTTP for HTTP.Builder;
14
12
using HTTP for HTTP.Request;
15
- using HTTPBuilder for HTTP.Request;
16
- using StringMap for StringMap.StringToStringMap;
17
13
using strings for * ;
18
14
using stdJson for string ;
19
15
20
- HTTP.Request req;
21
- StringMap.StringToStringMap headers;
22
- StringMap.StringToStringMap query;
16
+ HTTP.Builder http;
23
17
24
18
function test_HTTP_GET () public {
25
- req.withUrl ("https://jsonplaceholder.typicode.com/todos/1 " ).withMethod (HTTP.Method.GET);
26
- HTTP.Response memory res = req.request ();
19
+ HTTP.Response memory res = http.build ().GET ("https://jsonplaceholder.typicode.com/todos/1 " ).request ();
27
20
28
21
assertEq (res.status, 200 );
29
22
assertEq (res.data, '{ "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false} ' );
30
23
}
31
24
32
25
function test_HTTP_GET_options () public {
33
- req.withUrl ("https://httpbin.org/headers " ).withHeader ("accept " , "application/json " ).withHeader (
34
- "Authorization " , "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== "
35
- ).withMethod (HTTP.Method.GET);
36
- HTTP.Response memory res = req.request ();
26
+ HTTP.Response memory res = http.build ().GET ("https://httpbin.org/headers " ).withHeader (
27
+ "accept " , "application/json "
28
+ ).withHeader ("Authorization " , "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== " ).request ();
37
29
38
30
assertEq (res.status, 200 );
39
31
@@ -42,8 +34,8 @@ contract HTTPTest is Test {
42
34
}
43
35
44
36
function test_HTTP_POST_form_data () public {
45
- req. withUrl ( " https://httpbin.org/post " ). withMethod ( HTTP.
Method.POST). withBody ( " [email protected] " );
46
- HTTP.Response memory res = req .request ();
37
+ HTTP.Response memory res =
38
+ http. build (). POST ( " https://httpbin.org/post " ). withBody ( " [email protected] " ) .
request ();
47
39
48
40
assertEq (res.status, 200 );
49
41
@@ -52,40 +44,35 @@ contract HTTPTest is Test {
52
44
}
53
45
54
46
function test_HTTP_POST_json () public {
55
- req.withUrl ("https://httpbin.org/post " ).withMethod (HTTP.Method.POST).withBody ('{"foo": "bar"} ' );
56
- HTTP.Response memory res = req.request ();
47
+ HTTP.Response memory res = http.build ().POST ("https://httpbin.org/post " ).withBody ('{"foo": "bar"} ' ).request ();
57
48
58
49
assertEq (res.status, 200 );
59
50
assertTrue (res.data.toSlice ().contains (("foo " ).toSlice ()));
60
51
assertTrue (res.data.toSlice ().contains (("bar " ).toSlice ()));
61
52
}
62
53
63
54
function test_HTTP_PUT () public {
64
- req.withUrl ("https://httpbin.org/put " ).withMethod (HTTP.Method.PUT);
65
- HTTP.Response memory res = req.request ();
55
+ HTTP.Response memory res = http.build ().PUT ("https://httpbin.org/put " ).request ();
66
56
assertEq (res.status, 200 );
67
57
}
68
58
69
59
function test_HTTP_PUT_json () public {
70
- req. withUrl ( "https://httpbin.org/put " ). withMethod (HTTP.Method.PUT ).withBody ('{"foo": "bar"} ' ).withHeader (
60
+ HTTP.Response memory res = http. build (). PUT ( "https://httpbin.org/put " ).withBody ('{"foo": "bar"} ' ).withHeader (
71
61
"Content-Type " , "application/json "
72
- );
73
- HTTP.Response memory res = req.request ();
62
+ ).request ();
74
63
75
64
assertEq (res.status, 200 );
76
65
assertTrue (res.data.toSlice ().contains (('"foo" ' ).toSlice ()));
77
66
assertTrue (res.data.toSlice ().contains (('"bar" ' ).toSlice ()));
78
67
}
79
68
80
69
function test_HTTP_DELETE () public {
81
- req.withUrl ("https://httpbin.org/delete " ).withMethod (HTTP.Method.DELETE);
82
- HTTP.Response memory res = req.request ();
70
+ HTTP.Response memory res = http.build ().DELETE ("https://httpbin.org/delete " ).request ();
83
71
assertEq (res.status, 200 );
84
72
}
85
73
86
74
function test_HTTP_PATCH () public {
87
- req.withUrl ("https://httpbin.org/patch " ).withMethod (HTTP.Method.PATCH);
88
- HTTP.Response memory res = req.request ();
75
+ HTTP.Response memory res = http.build ().PATCH ("https://httpbin.org/patch " ).request ();
89
76
assertEq (res.status, 200 );
90
77
}
91
78
}
0 commit comments