3
3
#include < ESP8266HTTPClient.h>
4
4
#include < BSTest.h>
5
5
#include < test_config.h>
6
+ #include < pgmspace.h>
6
7
7
8
BS_ENV_DECLARE ();
8
9
@@ -17,16 +18,120 @@ void setup()
17
18
BS_RUN (Serial);
18
19
}
19
20
20
- TEST_CASE (" HTTP client GET request" , " [HTTPClient]" )
21
+ // json doesn't allow newlines within a string
22
+ auto test_payload = (const __FlashStringHelper *) " \" \
23
+ 0000000 53 48 45 4c 4c 20 3a 3d 20 2f 62 69 6e 2f 62 61\\ n\
24
+ 0000010 73 68 0a 56 20 3f 3d 20 30 0a 54 45 53 54 5f 4c\\ n\
25
+ 0000020 49 53 54 20 3f 3d 20 24 28 77 69 6c 64 63 61 72\\ n\
26
+ 0000030 64 20 74 65 73 74 5f 2a 2f 2a 2e 69 6e 6f 29 0a\\ n\
27
+ 0000040 45 53 50 38 32 36 36 5f 43 4f 52 45 5f 50 41 54\\ n\
28
+ 0000050 48 20 3f 3d 20 2e 2e 2f 2e 2e 0a 42 55 49 4c 44\\ n\
29
+ 0000060 5f 44 49 52 20 3f 3d 20 24 28 50 57 44 29 2f 2e\\ n\
30
+ 0000070 62 75 69 6c 64 0a 48 41 52 44 57 41 52 45 5f 44\\ n\
31
+ 0000080 49 52 20 3f 3d 20 24 28 50 57 44 29 2f 2e 68 61\\ n\
32
+ 0000090 72 64 77 61 72 65 0a 45 53 50 54 4f 4f 4c 20 3f\\ n\
33
+ 00000a0 3d 20 24 28 45 53 50 38 32 36 36 5f 43 4f 52 45\\ n\
34
+ 123\" " ;
35
+
36
+ auto bin_template_1 = (const __FlashStringHelper *) R"( {
37
+ "status": 200,
38
+ "statusText": "OK",
39
+ "httpVersion": "HTTP/1.1",
40
+ "headers": [],
41
+ "cookies": [],
42
+ "content": {
43
+ "mimeType": "text/plain",
44
+ "text":
45
+ )" ;
46
+
47
+ auto bin_template_2 = (const __FlashStringHelper *) R"(
48
+ }
49
+ })" ;
50
+
51
+ const char *mockbin_fingerprint = " F0 C1 7B 6A 89 4F AA 67 86 15 4A 17 6C 25 52 8E 2F F6 0F 34" ;
52
+
53
+ String createBin (bool https)
21
54
{
55
+ String payload;
56
+ payload.reserve (
57
+ strlen_P ((PGM_P) bin_template_1) +
58
+ strlen_P ((PGM_P) test_payload) +
59
+ strlen_P ((PGM_P) bin_template_2) + 1 );
60
+ payload += bin_template_1;
61
+ payload += test_payload;
62
+ payload += bin_template_2;
63
+
22
64
HTTPClient http;
23
- http.begin (" http://httpbin.org/get?a=1&b=asdadf" );
24
- auto httpCode = http.GET ();
25
- CHECK (httpCode == HTTP_CODE_OK);
26
- String payload = http.getString ();
27
- CHECK (payload.indexOf (" \" a\" : \" 1\" " ) != -1 );
28
- CHECK (payload.indexOf (" \" b\" : \" asdad\" " ) != -1 );
65
+ if (https) {
66
+ http.begin (" https://mockbin.org/bin/create" , mockbin_fingerprint);
67
+ }
68
+ else {
69
+ http.begin (" http://mockbin.org/bin/create" );
70
+ }
71
+ const char * location = " Location" ;
72
+ http.collectHeaders (&location, 1 );
73
+ http.addHeader (" Content-Type" , " application/json" );
74
+ auto result = http.POST (payload);
75
+ Serial.println (result);
76
+ Serial.println (" ----" );
77
+ Serial.println (http.getString ());
78
+ Serial.println (" ----" );
79
+ REQUIRE (result == 201 );
80
+ String url = " http://mockbin.org" + http.header (location);
29
81
http.end ();
82
+ return url;
83
+ }
84
+
85
+ TEST_CASE (" HTTP GET request" , " [HTTPClient]" )
86
+ {
87
+ const int repeatCount = 10 ;
88
+
89
+ String url = createBin (false );
90
+ int heapBefore = ESP.getFreeHeap ();
91
+ for (int i = 0 ; i < repeatCount; ++i) {
92
+ HTTPClient http;
93
+ http.begin (url);
94
+ auto httpCode = http.GET ();
95
+ REQUIRE (httpCode == HTTP_CODE_OK);
96
+ String payload = http.getString ();
97
+ payload.replace (" \n " , " \\ n" );
98
+ String quotedPayload;
99
+ quotedPayload.reserve (payload.length () + 3 );
100
+ quotedPayload += " \" " ;
101
+ quotedPayload += payload;
102
+ quotedPayload += " \" " ;
103
+ Serial.println (" ----payload:" );
104
+ Serial.println (quotedPayload);
105
+ Serial.println (" ----" );
106
+ Serial.println (" ----test_payload:" );
107
+ Serial.println (test_payload);
108
+ Serial.println (" ----" );
109
+ CHECK (quotedPayload == test_payload);
110
+ http.end ();
111
+ delay (100 );
112
+ }
113
+ int heapAfter = ESP.getFreeHeap ();
114
+ CHECK (heapBefore - heapAfter <= 8 );
115
+ }
116
+
117
+ TEST_CASE (" HTTPS GET request" , " [HTTPClient]" )
118
+ {
119
+ const int repeatCount = 10 ;
120
+
121
+ String url = createBin (true );
122
+ int heapBefore = ESP.getFreeHeap ();
123
+ for (int i = 0 ; i < repeatCount; ++i) {
124
+ HTTPClient http;
125
+ http.begin (url, mockbin_fingerprint);
126
+ auto httpCode = http.GET ();
127
+ REQUIRE (httpCode == HTTP_CODE_OK);
128
+ String payload = http.getString ();
129
+ CHECK (payload == test_payload);
130
+ http.end ();
131
+ delay (100 );
132
+ }
133
+ int heapAfter = ESP.getFreeHeap ();
134
+ CHECK (heapBefore - heapAfter <= 8 );
30
135
}
31
136
32
137
void loop ()
0 commit comments