1+ #include < unity.h>
2+
3+ #include < net/http_request.h>
4+
5+ using namespace dream ;
6+
7+ void setUp (void )
8+ {
9+ //
10+ }
11+
12+ void tearDown (void )
13+ {
14+ //
15+ }
16+
17+ void test_get (void )
18+ {
19+ HTTPRequest get = HTTPRequest (GET, " google.com" , " /" )
20+ .setUserAgent (" ESP" )
21+ .addHeader (" X-My-Header" , " value" );
22+
23+ String package_have_to_be;
24+ package_have_to_be += " GET / HTTP/1.1\r\n " ;
25+ package_have_to_be += " Host: google.com\r\n " ;
26+ package_have_to_be += " User-Agent: ESP\r\n " ;
27+ package_have_to_be += " X-My-Header: value\r\n " ;
28+ package_have_to_be += " Connection: close\r\n " ;
29+
30+ TEST_ASSERT_EQUAL_STRING (package_have_to_be.c_str (), get.toString ().c_str ());
31+ }
32+
33+ void test_post (void )
34+ {
35+ HTTPRequest post = HTTPRequest (POST, " eco-city.org.ua" , " /input" )
36+ .setUserAgent (" ESP" )
37+ .setContentType (" application/json" )
38+ .setAuthorization (" qwerty" , " 123456" )
39+ .setBody (" {\" data\" : true}" );
40+
41+ String package_have_to_be;
42+ package_have_to_be += " POST /input HTTP/1.1\r\n " ;
43+ package_have_to_be += " Host: eco-city.org.ua\r\n " ;
44+ package_have_to_be += " User-Agent: ESP\r\n " ;
45+ package_have_to_be += " Content-Type: application/json\r\n " ;
46+ package_have_to_be += " Authorization: Basic cXdlcnR5OjEyMzQ1Ng==\r\n " ;
47+ package_have_to_be += " Content-Length: 14\r\n " ;
48+ package_have_to_be += " Connection: close\r\n " ;
49+ package_have_to_be += " \r\n " ;
50+ package_have_to_be += " {\" data\" : true}\r\n " ;
51+
52+ TEST_ASSERT_EQUAL_STRING (package_have_to_be.c_str (), post .toString ().c_str ());
53+ }
54+
55+ void test_various_order (void )
56+ {
57+ HTTPRequest post = HTTPRequest (POST, " eco-city.org.ua" , " /input" )
58+ .setBody (" {\" data\" : true}" )
59+ .setContentType (" application/json" )
60+ .setUserAgent (" ESP" )
61+ .setAuthorization (" qwerty" , " 123456" );
62+
63+ String package_have_to_be;
64+ package_have_to_be += " POST /input HTTP/1.1\r\n " ;
65+ package_have_to_be += " Host: eco-city.org.ua\r\n " ;
66+ package_have_to_be += " Content-Length: 14\r\n " ;
67+ package_have_to_be += " Content-Type: application/json\r\n " ;
68+ package_have_to_be += " User-Agent: ESP\r\n " ;
69+ package_have_to_be += " Authorization: Basic cXdlcnR5OjEyMzQ1Ng==\r\n " ;
70+ package_have_to_be += " Connection: close\r\n " ;
71+ package_have_to_be += " \r\n " ;
72+ package_have_to_be += " {\" data\" : true}\r\n " ;
73+
74+ TEST_ASSERT_EQUAL_STRING (package_have_to_be.c_str (), post .toString ().c_str ());
75+ }
76+
77+ void setup ()
78+ {
79+ // NOTE!!! Wait for >2 secs
80+ // if board doesn't support software reset via Serial.DTR/RTS
81+ delay (2000 );
82+
83+ UNITY_BEGIN ();
84+ RUN_TEST (test_get);
85+ RUN_TEST (test_post);
86+ RUN_TEST (test_various_order);
87+
88+ UNITY_END ();
89+ }
90+
91+ void loop ()
92+ {
93+ delay (1000 );
94+ }
0 commit comments