66 "strconv"
77 "testing"
88
9+ b64 "encoding/base64"
910 "soarca/models/cacao"
1011 http "soarca/utils/http"
1112
@@ -36,11 +37,15 @@ func TestHttpGetConnection(t *testing.T) {
3637 httpRequest := http.HttpRequest {}
3738
3839 target := cacao.AgentTarget {HttpUrl : "https://httpbin.org/get" }
39- httpOptions := http. HttpOptions {
40- Method : "GET " ,
41- Target : target ,
40+ command := cacao. Command {
41+ Type : "http-api " ,
42+ Command : "GET / HTTP/1.1" ,
4243 Headers : map [string ]string {"accept" : "application/json" },
4344 }
45+ httpOptions := http.HttpOptions {
46+ Command : & command ,
47+ Target : & target ,
48+ }
4449 response , err := httpRequest .Request (httpOptions )
4550 t .Log (string (response ))
4651 if err != nil {
@@ -56,11 +61,15 @@ func TestHttpPostConnection(t *testing.T) {
5661 httpRequest := http.HttpRequest {}
5762
5863 target := cacao.AgentTarget {HttpUrl : "https://httpbin.org/post" }
59- httpOptions := http. HttpOptions {
60- Method : "POST " ,
61- Target : target ,
64+ command := cacao. Command {
65+ Type : "http-api " ,
66+ Command : "POST / HTTP/1.1" ,
6267 Headers : map [string ]string {"accept" : "application/json" },
6368 }
69+ httpOptions := http.HttpOptions {
70+ Command : & command ,
71+ Target : & target ,
72+ }
6473 response , err := httpRequest .Request (httpOptions )
6574 t .Log (string (response ))
6675 if err != nil {
@@ -75,11 +84,15 @@ func TestHttpPostConnection(t *testing.T) {
7584func TestHttpPutConnection (t * testing.T ) {
7685 httpRequest := http.HttpRequest {}
7786 target := cacao.AgentTarget {HttpUrl : "https://httpbin.org/put" }
78- httpOptions := http. HttpOptions {
79- Method : "PUT " ,
80- Target : target ,
87+ command := cacao. Command {
88+ Type : "http-api " ,
89+ Command : "PUT / HTTP/1.1" ,
8190 Headers : map [string ]string {"accept" : "application/json" },
8291 }
92+ httpOptions := http.HttpOptions {
93+ Command : & command ,
94+ Target : & target ,
95+ }
8396 response , err := httpRequest .Request (httpOptions )
8497 if err != nil {
8598 t .Fatalf (fmt .Sprint ("http put request test has failed: " , err ))
@@ -93,11 +106,15 @@ func TestHttpPutConnection(t *testing.T) {
93106func TestHttpDeleteConnection (t * testing.T ) {
94107 httpRequest := http.HttpRequest {}
95108 target := cacao.AgentTarget {HttpUrl : "https://httpbin.org/delete" }
96- httpOptions := http. HttpOptions {
97- Method : "DELETE " ,
98- Target : target ,
109+ command := cacao. Command {
110+ Type : "http-api " ,
111+ Command : "DELETE / HTTP/1.1" ,
99112 Headers : map [string ]string {"accept" : "application/json" },
100113 }
114+ httpOptions := http.HttpOptions {
115+ Command : & command ,
116+ Target : & target ,
117+ }
101118 response , err := httpRequest .Request (httpOptions )
102119 if err != nil {
103120 t .Fatalf (fmt .Sprint ("http delete request test has failed: " , err ))
@@ -113,11 +130,15 @@ func TestHttpDeleteConnection(t *testing.T) {
113130func TestHttpStatus200 (t * testing.T ) {
114131 httpRequest := http.HttpRequest {}
115132 target := cacao.AgentTarget {HttpUrl : "https://httpbin.org/status/200" }
116- httpOptions := http. HttpOptions {
117- Method : "GET " ,
118- Target : target ,
133+ command := cacao. Command {
134+ Type : "http-api " ,
135+ Command : "GET / HTTP/1.1" ,
119136 Headers : map [string ]string {"accept" : "application/json" },
120137 }
138+ httpOptions := http.HttpOptions {
139+ Command : & command ,
140+ Target : & target ,
141+ }
121142 // error codes are handled internally by request, with throw error != in 200 range
122143 _ , err := httpRequest .Request (httpOptions )
123144 t .Log (err )
@@ -131,10 +152,14 @@ func TestHttpBearerToken(t *testing.T) {
131152 bearerToken := "test"
132153 httpRequest := http.HttpRequest {}
133154 target := cacao.AgentTarget {HttpUrl : "https://httpbin.org/bearer" }
134- httpOptions := http. HttpOptions {
135- Method : "GET " ,
136- Target : target ,
155+ command := cacao. Command {
156+ Type : "http-api " ,
157+ Command : "GET / HTTP/1.1" ,
137158 Headers : map [string ]string {"accept" : "application/json" },
159+ }
160+ httpOptions := http.HttpOptions {
161+ Command : & command ,
162+ Target : & target ,
138163 Token : bearerToken ,
139164 }
140165 response , err := httpRequest .Request (httpOptions )
@@ -163,11 +188,14 @@ func TestHttpBasicAuth(t *testing.T) {
163188 httpRequest := http.HttpRequest {}
164189
165190 target := cacao.AgentTarget {HttpUrl : url }
166-
191+ command := cacao.Command {
192+ Type : "http-api" ,
193+ Command : "GET / HTTP/1.1" ,
194+ Headers : map [string ]string {"accept" : "application/json" },
195+ }
167196 httpOptions := http.HttpOptions {
168- Method : "GET" ,
169- Target : target ,
170- Headers : map [string ]string {"accept" : "application/json" },
197+ Command : & command ,
198+ Target : & target ,
171199 Username : username ,
172200 Password : password ,
173201 }
@@ -195,18 +223,23 @@ func TestHttpPostWithContentConnection(t *testing.T) {
195223
196224 testJsonObj := testJson {Id : "28818819" , User : "test" , Description : "very interesting description" }
197225 requestBody , err := json .Marshal (testJsonObj )
226+ base64EncodedBody := b64 .StdEncoding .EncodeToString (requestBody )
198227 if err != nil {
199228 fmt .Println ("error Marshall JSON:" , err )
200229 }
201230 if len (requestBody ) == 0 {
202231 t .Fatalf ("empty response" )
203232 }
204233 target := cacao.AgentTarget {HttpUrl : "https://httpbin.org/anything" }
234+ command := cacao.Command {
235+ Type : "http-api" ,
236+ Command : "POST / HTTP/1.1" ,
237+ Headers : map [string ]string {"accept" : "application/json" },
238+ ContentB64 : base64EncodedBody ,
239+ }
205240 httpOptions := http.HttpOptions {
206- Method : "POST" ,
207- Target : target ,
208- Headers : map [string ]string {"accept" : "application/json" },
209- Body : requestBody ,
241+ Command : & command ,
242+ Target : & target ,
210243 }
211244 response , err := httpRequest .Request (httpOptions )
212245
@@ -215,30 +248,32 @@ func TestHttpPostWithContentConnection(t *testing.T) {
215248 t .Fatalf (fmt .Sprint ("http post request with body content has failed: " , err ))
216249 }
217250
251+ // specific format used by httpbin.org
218252 var httpBinReponse httpBinResponseBody
219253 err = json .Unmarshal (response , & httpBinReponse )
254+ fmt .Println (httpBinReponse )
220255 if err != nil {
221- fmt .Println ("Error decoding JSON:" , err )
222256 t .Fatalf (fmt .Sprint ("Could not unmashall reponse token for bearer test," , err ))
223257 }
224-
225- var responseToken bearerResponse
226- err = json .Unmarshal ([]byte (httpBinReponse .Data ), & responseToken )
227- if err != nil {
228- fmt .Println ("Error decoding JSON:" , err )
229- t .Fatalf (fmt .Sprint ("Could not unmashall reponse token for bearer test," , err ))
230- }
231- assert .Equal (t , testJsonObj .Id , "28818819" )
232- assert .Equal (t , testJsonObj .User , "test" )
233- assert .Equal (t , testJsonObj .Description , "very interesting description" )
258+ assert .Equal (t , httpBinReponse .Data , base64EncodedBody )
234259}
235260
236261func TestHttpPathDnameParser (t * testing.T ) {
237262 addresses := make (map [string ][]string , 1 )
238263 addresses ["dname" ] = []string {"soarca.tno.nl" }
239264
240- testTarget := cacao.AgentTarget {Address : addresses , Port : strconv .Itoa (80 )}
241- parsedUrl , err := http .ExtractUrl (testTarget , "/url" )
265+ target := cacao.AgentTarget {Address : addresses , Port : strconv .Itoa (80 )}
266+ command := cacao.Command {
267+ Type : "http-api" ,
268+ Command : "POST /url HTTP/1.1" ,
269+ Headers : map [string ]string {"accept" : "application/json" },
270+ }
271+ httpOptions := http.HttpOptions {
272+ Target : & target ,
273+ Command : & command ,
274+ }
275+
276+ parsedUrl , err := httpOptions .ExtractUrl ()
242277 if err != nil {
243278 t .Fatalf (fmt .Sprint ("failed test because: " , err ))
244279 }
@@ -249,8 +284,18 @@ func TestHttpPathDnamePortParser(t *testing.T) {
249284 addresses := make (map [string ][]string , 1 )
250285 addresses ["dname" ] = []string {"soarca.tno.nl" }
251286
252- testTarget := cacao.AgentTarget {Address : addresses , Port : strconv .Itoa (8080 )}
253- parsedUrl , err := http .ExtractUrl (testTarget , "/url" )
287+ target := cacao.AgentTarget {Address : addresses , Port : strconv .Itoa (8080 )}
288+ command := cacao.Command {
289+ Type : "http-api" ,
290+ Command : "POST /url HTTP/1.1" ,
291+ Headers : map [string ]string {"accept" : "application/json" },
292+ }
293+ httpOptions := http.HttpOptions {
294+ Target : & target ,
295+ Command : & command ,
296+ }
297+
298+ parsedUrl , err := httpOptions .ExtractUrl ()
254299 if err != nil {
255300 t .Fatalf (fmt .Sprint ("failed test because: " , err ))
256301 }
@@ -259,10 +304,20 @@ func TestHttpPathDnamePortParser(t *testing.T) {
259304
260305func TestHttpPathDnameRandomPortParser (t * testing.T ) {
261306 addresses := make (map [string ][]string , 1 )
262- addresses ["dname" ] = []string {"soarca.tno.nl" }
307+ addresses ["dname" ] = []string {"http://soarca.tno.nl" }
308+
309+ target := cacao.AgentTarget {Address : addresses , Port : strconv .Itoa (6464 )}
310+ command := cacao.Command {
311+ Type : "http-api" ,
312+ Command : "POST /url HTTP/1.1" ,
313+ Headers : map [string ]string {"accept" : "application/json" },
314+ }
315+ httpOptions := http.HttpOptions {
316+ Target : & target ,
317+ Command : & command ,
318+ }
263319
264- testTarget := cacao.AgentTarget {Address : addresses , Port : strconv .Itoa (6464 )}
265- parsedUrl , err := http .ExtractUrl (testTarget , "/url" )
320+ parsedUrl , err := httpOptions .ExtractUrl ()
266321 if err != nil {
267322 t .Fatalf (fmt .Sprint ("failed test because: " , err ))
268323 }
@@ -273,28 +328,110 @@ func TestHttpPathIpv4Parser(t *testing.T) {
273328 addresses := make (map [string ][]string , 1 )
274329 addresses ["ipv4" ] = []string {"127.0.0.1" }
275330
276- testTarget := cacao.AgentTarget {Address : addresses , Port : strconv .Itoa (443 )}
277- parsedUrl , err := http .ExtractUrl (testTarget , "" )
331+ target := cacao.AgentTarget {Address : addresses , Port : strconv .Itoa (443 )}
332+ command := cacao.Command {
333+ Type : "http-api" ,
334+ Command : "POST / HTTP/1.1" ,
335+ Headers : map [string ]string {"accept" : "application/json" },
336+ }
337+ httpOptions := http.HttpOptions {
338+ Target : & target ,
339+ Command : & command ,
340+ }
341+
342+ parsedUrl , err := httpOptions .ExtractUrl ()
278343 if err != nil {
279344 t .Fatalf (fmt .Sprint ("failed test because: " , err ))
280345 }
281- assert .Equal (t , parsedUrl , "https://127.0.0.1:443" )
346+ assert .Equal (t , parsedUrl , "https://127.0.0.1:443/ " )
282347}
283348
284349func TestHttpPathParser (t * testing.T ) {
285- testTarget := cacao.AgentTarget {HttpUrl : "https://godcapability.tno.nl" }
286- parsedUrl , err := http .ExtractUrl (testTarget , "" )
350+ target := cacao.AgentTarget {HttpUrl : "https://godcapability.tno.nl" }
351+ command := cacao.Command {
352+ Type : "http-api" ,
353+ Command : "POST / HTTP/1.1" ,
354+ Headers : map [string ]string {"accept" : "application/json" },
355+ }
356+ httpOptions := http.HttpOptions {
357+ Target : & target ,
358+ Command : & command ,
359+ }
360+
361+ parsedUrl , err := httpOptions .ExtractUrl ()
287362 if err != nil {
288363 t .Fatalf (fmt .Sprint ("failed test because: " , err ))
289364 }
290365 assert .Equal (t , parsedUrl , "https://godcapability.tno.nl" )
291366}
292367
293368func TestHttpPathBreakingParser (t * testing.T ) {
294- testTarget := cacao.AgentTarget {HttpUrl : "https://" }
295- parsedUrl , err := http .ExtractUrl (testTarget , "" )
369+ target := cacao.AgentTarget {HttpUrl : "https://" }
370+ command := cacao.Command {
371+ Type : "http-api" ,
372+ Command : "POST / HTTP/1.1" ,
373+ Headers : map [string ]string {"accept" : "application/json" },
374+ }
375+ httpOptions := http.HttpOptions {
376+ Target : & target ,
377+ Command : & command ,
378+ }
379+
380+ parsedUrl , err := httpOptions .ExtractUrl ()
296381 if err == nil {
297382 t .Error ("want error for invalid empty domainname" )
298383 }
299384 t .Logf (fmt .Sprint (parsedUrl ))
300385}
386+
387+ func TestMethodExtract (t * testing.T ) {
388+ command := cacao.Command {
389+ Type : "http-api" ,
390+ Command : "POST /api1/newObject HTTP/1.1" ,
391+ Headers : map [string ]string {"accept" : "application/json" },
392+ }
393+ method , err := http .GetMethodFrom (& command )
394+ if err != nil {
395+ t .Fatalf (fmt .Sprint ("failed test because: " , err ))
396+ }
397+ assert .Equal (t , method , "POST" )
398+ }
399+
400+ func TestPathExtract (t * testing.T ) {
401+ command := cacao.Command {
402+ Type : "http-api" ,
403+ Command : "POST /api1/newObject HTTP/1.1" ,
404+ Headers : map [string ]string {"accept" : "application/json" },
405+ }
406+ path , err := http .GetPathFrom (& command )
407+ if err != nil {
408+ t .Fatalf (fmt .Sprint ("failed test because: " , err ))
409+ }
410+ assert .Equal (t , path , "/api1/newObject" )
411+ }
412+
413+ func TestVersionExtract (t * testing.T ) {
414+ command := cacao.Command {
415+ Type : "http-api" ,
416+ Command : "POST /api1/newObject HTTP/1.1" ,
417+ Headers : map [string ]string {"accept" : "application/json" },
418+ }
419+ version , err := http .GetVersionFrom (& command )
420+ if err != nil {
421+ t .Fatalf (fmt .Sprint ("failed test because: " , err ))
422+ }
423+ assert .Equal (t , version , "HTTP/1.1" )
424+ }
425+
426+ func TestCommandFailedExtract (t * testing.T ) {
427+ command := cacao.Command {
428+ Type : "http-api" ,
429+ Command : "POST /api1/newObject" ,
430+ Headers : map [string ]string {"accept" : "application/json" },
431+ }
432+ version , err := http .GetVersionFrom (& command )
433+ if err == nil {
434+ t .Error ("should give error as only 2 values are provided" )
435+ }
436+ assert .Equal (t , version , "" )
437+ }
0 commit comments