Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
danyalprout committed Feb 27, 2024
1 parent 0227084 commit d87e315
Showing 1 changed file with 49 additions and 26 deletions.
75 changes: 49 additions & 26 deletions api/service/api_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package service

import (
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"net/http/httptest"
"os"
"testing"
Expand Down Expand Up @@ -228,38 +230,59 @@ func TestAPIService(t *testing.T) {

for _, test := range tests {
for _, rf := range responseFormat {
t.Run(fmt.Sprintf("%s-%s", test.name, rf), func(t *testing.T) {
request := httptest.NewRequest("GET", test.path, nil)
request.Header.Set("Accept", rf)
for _, compress := range []bool{true, false} {
testName := fmt.Sprintf("%s-%s", test.name, rf)
if compress {
testName = fmt.Sprintf("%s-%s", testName, "gzip")
}

response := httptest.NewRecorder()
t.Run(testName, func(t *testing.T) {
request := httptest.NewRequest("GET", test.path, nil)
request.Header.Set("Accept", rf)

a.router.ServeHTTP(response, request)
if compress {
request.Header.Set("Accept-Encoding", "gzip")
}

require.Equal(t, test.status, response.Code)
response := httptest.NewRecorder()

if test.status == 200 && test.expected != nil {
blobSidecars := storage.BlobSidecars{}
a.router.ServeHTTP(response, request)

var err error
if rf == "application/octet-stream" {
res := api.BlobSidecars{}
err = res.UnmarshalSSZ(response.Body.Bytes())
blobSidecars.Data = res.Sidecars
} else {
err = json.Unmarshal(response.Body.Bytes(), &blobSidecars)
}
require.Equal(t, test.status, response.Code)

require.NoError(t, err)
require.Equal(t, *test.expected, blobSidecars)
} else if test.status != 200 && rf == "application/json" && test.errMessage != "" {
var e httpError
err := json.Unmarshal(response.Body.Bytes(), &e)
require.NoError(t, err)
require.Equal(t, test.status, e.Code)
require.Equal(t, test.errMessage, e.Message)
}
})
if test.status == 200 && test.expected != nil {
var data []byte
if compress {
reader, err := gzip.NewReader(response.Body)
require.NoError(t, err)

data, err = io.ReadAll(reader)
require.NoError(t, err)
} else {
data = response.Body.Bytes()
}

blobSidecars := storage.BlobSidecars{}

if rf == "application/octet-stream" {
res := api.BlobSidecars{}
err = res.UnmarshalSSZ(data)
blobSidecars.Data = res.Sidecars
} else {
err = json.Unmarshal(data, &blobSidecars)
}

require.NoError(t, err)
require.Equal(t, *test.expected, blobSidecars)
} else if test.status != 200 && rf == "application/json" && test.errMessage != "" {
var e httpError
err := json.Unmarshal(response.Body.Bytes(), &e)
require.NoError(t, err)
require.Equal(t, test.status, e.Code)
require.Equal(t, test.errMessage, e.Message)
}
})
}
}
}
}
Expand Down

0 comments on commit d87e315

Please sign in to comment.