Skip to content

Commit 0421913

Browse files
randomshinichigbalint
authored andcommitted
swarm/api/http: GET/PUT/PATCH/DELETE/POST multipart form unit tests. (#17277)
httpDo has a verbose option that dumps the HTTP request
1 parent 93fe16b commit 0421913

1 file changed

Lines changed: 268 additions & 0 deletions

File tree

‎swarm/api/http/server_test.go‎

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import (
2727
"fmt"
2828
"io"
2929
"io/ioutil"
30+
"mime/multipart"
3031
"net/http"
3132
"os"
33+
"strconv"
3234
"strings"
3335
"testing"
3436
"time"
@@ -906,3 +908,269 @@ func TestMethodsNotAllowed(t *testing.T) {
906908
}
907909

908910
}
911+
912+
// HTTP convenience function
913+
func httpDo(httpMethod string, url string, reqBody io.Reader, headers map[string]string, verbose bool, t *testing.T) (*http.Response, string) {
914+
// Build the Request
915+
req, err := http.NewRequest(httpMethod, url, reqBody)
916+
if err != nil {
917+
t.Fatal(err)
918+
}
919+
for key, value := range headers {
920+
req.Header.Set(key, value)
921+
}
922+
if verbose {
923+
t.Log(req.Method, req.URL, req.Header, req.Body)
924+
}
925+
926+
// Send Request out
927+
httpClient := &http.Client{}
928+
res, err := httpClient.Do(req)
929+
if err != nil {
930+
t.Fatal(err)
931+
}
932+
933+
// Read the HTTP Body
934+
buffer, err := ioutil.ReadAll(res.Body)
935+
if err != nil {
936+
t.Fatal(err)
937+
}
938+
defer res.Body.Close()
939+
body := string(buffer)
940+
941+
return res, body
942+
}
943+
944+
func TestGet(t *testing.T) {
945+
// Setup Swarm
946+
srv := testutil.NewTestSwarmServer(t, serverFunc)
947+
defer srv.Close()
948+
949+
testCases := []struct {
950+
uri string
951+
method string
952+
headers map[string]string
953+
expectedStatusCode int
954+
assertResponseBody string
955+
verbose bool
956+
}{
957+
{
958+
// Accept: text/html GET / -> 200 HTML, Swarm Landing Page
959+
uri: fmt.Sprintf("%s/", srv.URL),
960+
method: "GET",
961+
headers: map[string]string{"Accept": "text/html"},
962+
expectedStatusCode: 200,
963+
assertResponseBody: "<a href=\"/bzz:/theswarm.eth\">Swarm</a>: Serverless Hosting Incentivised peer-to-peer Storage and Content Distribution",
964+
verbose: false,
965+
},
966+
{
967+
// Accept: application/json GET / -> 200 'Welcome to Swarm'
968+
uri: fmt.Sprintf("%s/", srv.URL),
969+
method: "GET",
970+
headers: map[string]string{"Accept": "application/json"},
971+
expectedStatusCode: 200,
972+
assertResponseBody: "Welcome to Swarm!",
973+
verbose: false,
974+
},
975+
{
976+
// GET /robots.txt -> 200
977+
uri: fmt.Sprintf("%s/robots.txt", srv.URL),
978+
method: "GET",
979+
headers: map[string]string{"Accept": "text/html"},
980+
expectedStatusCode: 200,
981+
assertResponseBody: "User-agent: *\nDisallow: /",
982+
verbose: false,
983+
},
984+
{
985+
// GET /path_that_doesnt exist -> 400
986+
uri: fmt.Sprintf("%s/nonexistent_path", srv.URL),
987+
method: "GET",
988+
headers: map[string]string{},
989+
expectedStatusCode: 400,
990+
verbose: false,
991+
},
992+
{
993+
// GET bzz-invalid:/ -> 400
994+
uri: fmt.Sprintf("%s/bzz:asdf/", srv.URL),
995+
method: "GET",
996+
headers: map[string]string{},
997+
expectedStatusCode: 400,
998+
verbose: false,
999+
},
1000+
{
1001+
// GET bzz-invalid:/ -> 400
1002+
uri: fmt.Sprintf("%s/tbz2/", srv.URL),
1003+
method: "GET",
1004+
headers: map[string]string{},
1005+
expectedStatusCode: 400,
1006+
verbose: false,
1007+
},
1008+
{
1009+
// GET bzz-invalid:/ -> 400
1010+
uri: fmt.Sprintf("%s/bzz-rack:/", srv.URL),
1011+
method: "GET",
1012+
headers: map[string]string{},
1013+
expectedStatusCode: 400,
1014+
verbose: false,
1015+
},
1016+
{
1017+
// GET bzz-invalid:/ -> 400
1018+
uri: fmt.Sprintf("%s/bzz-ls", srv.URL),
1019+
method: "GET",
1020+
headers: map[string]string{},
1021+
expectedStatusCode: 400,
1022+
verbose: false,
1023+
},
1024+
}
1025+
1026+
for _, testCase := range testCases {
1027+
t.Run("GET "+testCase.uri, func(t *testing.T) {
1028+
res, body := httpDo(testCase.method, testCase.uri, nil, testCase.headers, testCase.verbose, t)
1029+
if res.StatusCode != testCase.expectedStatusCode {
1030+
t.Fatalf("expected %s %s to return a %v but it didn't", testCase.method, testCase.uri, testCase.expectedStatusCode)
1031+
}
1032+
if testCase.assertResponseBody != "" && !strings.Contains(body, testCase.assertResponseBody) {
1033+
t.Fatalf("expected %s %s to have %s within HTTP response body but it didn't", testCase.method, testCase.uri, testCase.assertResponseBody)
1034+
}
1035+
})
1036+
}
1037+
}
1038+
1039+
func TestModify(t *testing.T) {
1040+
// Setup Swarm and upload a test file to it
1041+
srv := testutil.NewTestSwarmServer(t, serverFunc)
1042+
defer srv.Close()
1043+
1044+
swarmClient := swarm.NewClient(srv.URL)
1045+
data := []byte("data")
1046+
file := &swarm.File{
1047+
ReadCloser: ioutil.NopCloser(bytes.NewReader(data)),
1048+
ManifestEntry: api.ManifestEntry{
1049+
Path: "",
1050+
ContentType: "text/plain",
1051+
Size: int64(len(data)),
1052+
},
1053+
}
1054+
1055+
hash, err := swarmClient.Upload(file, "", false)
1056+
if err != nil {
1057+
t.Fatal(err)
1058+
}
1059+
1060+
testCases := []struct {
1061+
uri string
1062+
method string
1063+
headers map[string]string
1064+
requestBody []byte
1065+
expectedStatusCode int
1066+
assertResponseBody string
1067+
assertResponseHeaders map[string]string
1068+
verbose bool
1069+
}{
1070+
{
1071+
// DELETE bzz:/hash -> 200 OK
1072+
uri: fmt.Sprintf("%s/bzz:/%s", srv.URL, hash),
1073+
method: "DELETE",
1074+
headers: map[string]string{},
1075+
expectedStatusCode: 200,
1076+
assertResponseBody: "8b634aea26eec353ac0ecbec20c94f44d6f8d11f38d4578a4c207a84c74ef731",
1077+
verbose: false,
1078+
},
1079+
{
1080+
// PUT bzz:/hash -> 405 Method Not Allowed
1081+
uri: fmt.Sprintf("%s/bzz:/%s", srv.URL, hash),
1082+
method: "PUT",
1083+
headers: map[string]string{},
1084+
expectedStatusCode: 405,
1085+
verbose: false,
1086+
},
1087+
{
1088+
// PUT bzz-raw:/hash -> 405 Method Not Allowed
1089+
uri: fmt.Sprintf("%s/bzz-raw:/%s", srv.URL, hash),
1090+
method: "PUT",
1091+
headers: map[string]string{},
1092+
expectedStatusCode: 405,
1093+
verbose: false,
1094+
},
1095+
{
1096+
// PATCH bzz:/hash -> 405 Method Not Allowed
1097+
uri: fmt.Sprintf("%s/bzz:/%s", srv.URL, hash),
1098+
method: "PATCH",
1099+
headers: map[string]string{},
1100+
expectedStatusCode: 405,
1101+
verbose: false,
1102+
},
1103+
{
1104+
// POST bzz-raw:/ -> 200 OK
1105+
uri: fmt.Sprintf("%s/bzz-raw:/", srv.URL),
1106+
method: "POST",
1107+
headers: map[string]string{},
1108+
requestBody: []byte("POSTdata"),
1109+
expectedStatusCode: 200,
1110+
assertResponseHeaders: map[string]string{"Content-Length": "64"},
1111+
verbose: false,
1112+
},
1113+
{
1114+
// POST bzz-raw:/encrypt -> 200 OK
1115+
uri: fmt.Sprintf("%s/bzz-raw:/encrypt", srv.URL),
1116+
method: "POST",
1117+
headers: map[string]string{},
1118+
requestBody: []byte("POSTdata"),
1119+
expectedStatusCode: 200,
1120+
assertResponseHeaders: map[string]string{"Content-Length": "128"},
1121+
verbose: false,
1122+
},
1123+
}
1124+
1125+
for _, testCase := range testCases {
1126+
t.Run(testCase.method+" "+testCase.uri, func(t *testing.T) {
1127+
reqBody := bytes.NewReader(testCase.requestBody)
1128+
res, body := httpDo(testCase.method, testCase.uri, reqBody, testCase.headers, testCase.verbose, t)
1129+
1130+
if res.StatusCode != testCase.expectedStatusCode {
1131+
t.Fatalf("expected %s %s to return a %v but it returned a %v instead", testCase.method, testCase.uri, testCase.expectedStatusCode, res.StatusCode)
1132+
}
1133+
if testCase.assertResponseBody != "" && !strings.Contains(body, testCase.assertResponseBody) {
1134+
t.Log(body)
1135+
t.Fatalf("expected %s %s to have %s within HTTP response body but it didn't", testCase.method, testCase.uri, testCase.assertResponseBody)
1136+
}
1137+
for key, value := range testCase.assertResponseHeaders {
1138+
if res.Header.Get(key) != value {
1139+
t.Logf("expected %s=%s in HTTP response header but got %s", key, value, res.Header.Get(key))
1140+
}
1141+
}
1142+
})
1143+
}
1144+
}
1145+
1146+
func TestMultiPartUpload(t *testing.T) {
1147+
// POST /bzz:/ Content-Type: multipart/form-data
1148+
verbose := false
1149+
// Setup Swarm
1150+
srv := testutil.NewTestSwarmServer(t, serverFunc)
1151+
defer srv.Close()
1152+
1153+
url := fmt.Sprintf("%s/bzz:/", srv.URL)
1154+
1155+
buf := new(bytes.Buffer)
1156+
form := multipart.NewWriter(buf)
1157+
form.WriteField("name", "John Doe")
1158+
file1, _ := form.CreateFormFile("cv", "cv.txt")
1159+
file1.Write([]byte("John Doe's Credentials"))
1160+
file2, _ := form.CreateFormFile("profile_picture", "profile.jpg")
1161+
file2.Write([]byte("imaginethisisjpegdata"))
1162+
form.Close()
1163+
1164+
headers := map[string]string{
1165+
"Content-Type": form.FormDataContentType(),
1166+
"Content-Length": strconv.Itoa(buf.Len()),
1167+
}
1168+
res, body := httpDo("POST", url, buf, headers, verbose, t)
1169+
1170+
if res.StatusCode != 200 {
1171+
t.Fatalf("expected POST multipart/form-data to return 200, but it returned %d", res.StatusCode)
1172+
}
1173+
if len(body) != 64 {
1174+
t.Fatalf("expected POST multipart/form-data to return a 64 char manifest but the answer was %d chars long", len(body))
1175+
}
1176+
}

0 commit comments

Comments
 (0)