Skip to content

Commit

Permalink
Added http content usage in http request (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaartendeKruijf authored May 6, 2024
1 parent 98587da commit 96efcf8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
54 changes: 53 additions & 1 deletion test/unittest/utils/http/http_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ssh_test

import (
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
Expand Down Expand Up @@ -262,6 +263,52 @@ func TestHttpBasicAuth(t *testing.T) {
func TestHttpPostWithContentConnection(t *testing.T) {
httpRequest := http.HttpRequest{}

testJsonObj := testJson{Id: "28818819", User: "test", Description: "very interesting description"}
requestBody, err := json.Marshal(testJsonObj)
body := "some payload body"
if err != nil {
t.Error("error Marshall JSON: ", err)
}
if len(requestBody) == 0 {
t.Error("empty response")
}

target := cacao.AgentTarget{
Address: map[cacao.NetAddressType][]string{
"url": {"https://httpbin.org/anything"},
},
}

command := cacao.Command{
Type: "http-api",
Command: "POST / HTTP/1.1",
Headers: map[string][]string{"accept": {"application/json"}},
Content: body,
}
httpOptions := http.HttpOptions{
Command: &command,
Target: &target,
}
response, err := httpRequest.Request(httpOptions)

t.Log(string(response))
if err != nil {
t.Error("http post request with body content has failed: ", err)
}

// specific format used by httpbin.org
var httpBinReponse httpBinResponseBody
err = json.Unmarshal(response, &httpBinReponse)
fmt.Println(httpBinReponse)
if err != nil {
t.Error("Could not unmashall reponse token for bearer test,", err)
}
assert.Equal(t, httpBinReponse.Data, body)
}

func TestHttpPostWithBase64ContentConnection(t *testing.T) {
httpRequest := http.HttpRequest{}

testJsonObj := testJson{Id: "28818819", User: "test", Description: "very interesting description"}
requestBody, err := json.Marshal(testJsonObj)
base64EncodedBody := b64.StdEncoding.EncodeToString(requestBody)
Expand Down Expand Up @@ -302,7 +349,12 @@ func TestHttpPostWithContentConnection(t *testing.T) {
if err != nil {
t.Error("Could not unmashall reponse token for bearer test,", err)
}
assert.Equal(t, httpBinReponse.Data, base64EncodedBody)
decodedObject, err := base64.StdEncoding.DecodeString(base64EncodedBody)
if err != nil {
t.Log(err)
t.Fail()
}
assert.Equal(t, httpBinReponse.Data, string(decodedObject))
}

func TestHttpPathDnameParser(t *testing.T) {
Expand Down
19 changes: 18 additions & 1 deletion utils/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -51,6 +52,7 @@ func (httpRequest *HttpRequest) Request(httpOptions HttpOptions) ([]byte, error)
}

client := &http.Client{}
log.Trace(request)
response, err := client.Do(request)
if err != nil {
log.Error(err)
Expand All @@ -71,7 +73,22 @@ func (httpOptions *HttpOptions) setupRequest() (*http.Request, error) {
log.Error(err)
return nil, err
}
requestBuffer := bytes.NewBufferString(httpOptions.Command.ContentB64)

requestBuffer := bytes.NewBufferString("")
if httpOptions.Command.Content != "" {
log.Debug("using the content field")
requestBuffer = bytes.NewBufferString(httpOptions.Command.Content)
} else if httpOptions.Command.ContentB64 != "" {
log.Debug("using base64 content")
byteString, err := base64.StdEncoding.DecodeString(httpOptions.Command.ContentB64)
if err != nil {
log.Error("error decoding base64", err)
} else {
log.Trace(string(byteString))
requestBuffer = bytes.NewBufferString(string(byteString))
}
}
log.Trace("request buffer is: ", requestBuffer)
request, err := http.NewRequest(method, parsedUrl, requestBuffer)
if err != nil {
log.Error(err)
Expand Down

0 comments on commit 96efcf8

Please sign in to comment.