Commit d6667744 authored by Nemanja Zbiljić's avatar Nemanja Zbiljić Committed by GitHub

Simplify JSON response extraction in tests (#1085)

parent 781f7330
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
"archive/tar" "archive/tar"
"bytes" "bytes"
"context" "context"
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
...@@ -279,12 +278,12 @@ Disallow: /`), ...@@ -279,12 +278,12 @@ Disallow: /`),
// tar all the test case files // tar all the test case files
tarReader := tarFiles(t, tc.files) tarReader := tarFiles(t, tc.files)
var respBytes []byte var resp api.FileUploadResponse
options := []jsonhttptest.Option{ options := []jsonhttptest.Option{
jsonhttptest.WithRequestBody(tarReader), jsonhttptest.WithRequestBody(tarReader),
jsonhttptest.WithRequestHeader("Content-Type", api.ContentTypeTar), jsonhttptest.WithRequestHeader("Content-Type", api.ContentTypeTar),
jsonhttptest.WithPutResponseBody(&respBytes), jsonhttptest.WithUnmarshalJSONResponse(&resp),
} }
if tc.indexFilenameOption != nil { if tc.indexFilenameOption != nil {
options = append(options, tc.indexFilenameOption) options = append(options, tc.indexFilenameOption)
...@@ -299,15 +298,6 @@ Disallow: /`), ...@@ -299,15 +298,6 @@ Disallow: /`),
// verify directory tar upload response // verify directory tar upload response
jsonhttptest.Request(t, client, http.MethodPost, dirUploadResource, http.StatusOK, options...) jsonhttptest.Request(t, client, http.MethodPost, dirUploadResource, http.StatusOK, options...)
read := bytes.NewReader(respBytes)
// get the reference
var resp api.FileUploadResponse
err := json.NewDecoder(read).Decode(&resp)
if err != nil {
t.Fatal(err)
}
if resp.Reference.String() == "" { if resp.Reference.String() == "" {
t.Fatalf("expected file reference, did not got any") t.Fatalf("expected file reference, did not got any")
} }
......
...@@ -346,12 +346,9 @@ func TestRangeRequests(t *testing.T) { ...@@ -346,12 +346,9 @@ func TestRangeRequests(t *testing.T) {
uploadReference := upload.reference uploadReference := upload.reference
var respBytes []byte
jsonhttptest.Request(t, client, http.MethodPost, upload.uploadEndpoint, http.StatusOK, jsonhttptest.Request(t, client, http.MethodPost, upload.uploadEndpoint, http.StatusOK,
jsonhttptest.WithRequestBody(upload.reader), jsonhttptest.WithRequestBody(upload.reader),
jsonhttptest.WithRequestHeader("Content-Type", upload.contentType), jsonhttptest.WithRequestHeader("Content-Type", upload.contentType),
jsonhttptest.WithPutResponseBody(&respBytes),
) )
for _, tc := range ranges { for _, tc := range ranges {
......
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
package api_test package api_test
import ( import (
"bytes"
"encoding/json"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"testing" "testing"
...@@ -71,20 +69,13 @@ func TestPinBzzHandler(t *testing.T) { ...@@ -71,20 +69,13 @@ func TestPinBzzHandler(t *testing.T) {
expectedChunkCount := 7 expectedChunkCount := 7
var respBytes []byte // get the reference as everytime it will change because of random encryption key
var resp api.ListPinnedChunksResponse
jsonhttptest.Request(t, client, http.MethodGet, pinChunksResource, http.StatusOK, jsonhttptest.Request(t, client, http.MethodGet, pinChunksResource, http.StatusOK,
jsonhttptest.WithPutResponseBody(&respBytes), jsonhttptest.WithUnmarshalJSONResponse(&resp),
) )
read := bytes.NewReader(respBytes)
var resp api.ListPinnedChunksResponse
err := json.NewDecoder(read).Decode(&resp)
if err != nil {
t.Fatal(err)
}
if expectedChunkCount != len(resp.Chunks) { if expectedChunkCount != len(resp.Chunks) {
t.Fatalf("expected to find %d pinned chunks, got %d", expectedChunkCount, len(resp.Chunks)) t.Fatalf("expected to find %d pinned chunks, got %d", expectedChunkCount, len(resp.Chunks))
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment