Skip to content

Commit

Permalink
Use seed from query params and header
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Dec 1, 2024
1 parent 8383184 commit a3eac92
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
2 changes: 1 addition & 1 deletion internal/handler/mock/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (h *Handler) writeResponse(writer contracts.ResponseWriter, request *contra

switch {
case response.IsFake():
if err := h.serveFakeContent(writer); err != nil {
if err := h.serveFakeContent(writer, request); err != nil {
return err
}
case response.IsFile():
Expand Down
43 changes: 41 additions & 2 deletions internal/handler/mock/serve_fake_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@ package mock

import (
"encoding/json"
"errors"
"strconv"

"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/internal/contracts"
"github.com/go-http-utils/headers"
)

func (h *Handler) serveFakeContent(writer contracts.ResponseWriter) error {
const seedQueryParamName = "$__uncors__seed"

func (h *Handler) serveFakeContent(writer contracts.ResponseWriter, request *contracts.Request) error {
response := h.response
header := writer.Header()

if len(header.Get(headers.ContentType)) == 0 {
header.Set(headers.ContentType, "application/json")
}

data, err := response.Fake.Compile(response.Seed)
seed, err := extractSeed(response, request)
if err != nil {
return err
}

data, err := response.Fake.Compile(seed)
if err != nil {
return err
}
Expand All @@ -25,3 +35,32 @@ func (h *Handler) serveFakeContent(writer contracts.ResponseWriter) error {
return json.NewEncoder(writer).
Encode(data)
}

func extractSeed(response config.Response, request *contracts.Request) (uint64, error) {
if response.Seed > 0 {
return response.Seed, nil
}

queries := request.URL.Query()
if queries.Has(seedQueryParamName) {
return parseUint(queries.Get(seedQueryParamName))
}

header := request.Header.Get(seedQueryParamName)
if header != "" {
return parseUint(header)
}

return 0, nil
}

var ErrInvalidSeed = errors.New("invalid $__uncors__seed parameter")

func parseUint(value string) (uint64, error) {
seed, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return 0, errors.Join(ErrInvalidSeed, err)
}

return seed, nil
}
77 changes: 77 additions & 0 deletions internal/handler/mock/serve_fake_content_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package mock_test

import (
"github.com/stretchr/testify/assert"

Check failure on line 4 in internal/handler/mock/serve_fake_content_test.go

View workflow job for this annotation

GitHub Actions / Lint, Build and Test

File is not `gofumpt`-ed (gofumpt)
"net/http"
"net/http/httptest"
"testing"

Check failure on line 7 in internal/handler/mock/serve_fake_content_test.go

View workflow job for this annotation

GitHub Actions / Lint, Build and Test

File is not `gofumpt`-ed (gofumpt)

Check failure on line 8 in internal/handler/mock/serve_fake_content_test.go

View workflow job for this annotation

GitHub Actions / Lint, Build and Test

File is not `goimports`-ed (goimports)
"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/internal/contracts"
"github.com/evg4b/uncors/internal/handler/mock"
"github.com/evg4b/uncors/pkg/fakedata"
"github.com/evg4b/uncors/testing/mocks"
)

func TestFakeResponse(t *testing.T) {
loggerMock := mocks.NewLoggerMock(t).
PrintMock.Return()

handler := mock.NewMockHandler(
mock.WithLogger(loggerMock),
mock.WithResponse(config.Response{
Code: http.StatusOK,
Fake: &fakedata.Node{
Type: "object",
Properties: map[string]fakedata.Node{
"hello": {
Type: "string",
Options: map[string]interface{}{
"wordcount": 3,
},
},
"world": {
Type: "string",
Options: map[string]interface{}{
"wordcount": 3,
},
},
},
},
}),
)

t.Run("seed from query", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/hello?$__uncors__seed=123", nil)

responseRecorder := httptest.NewRecorder()
handler.ServeHTTP(contracts.WrapResponseWriter(responseRecorder), req)

assert.Equal(t, responseRecorder.Code, http.StatusOK)

Check failure on line 50 in internal/handler/mock/serve_fake_content_test.go

View workflow job for this annotation

GitHub Actions / Lint, Build and Test

expected-actual: need to reverse actual and expected values (testifylint)
actual := responseRecorder.Body.String()
assert.Equal(t, "{\"hello\":\"At esse ea.\",\"world\":\"Sint ut culpa.\"}\n", actual)
})

t.Run("seed from header", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/hello", nil)
req.Header.Set("$__uncors__seed", "123")

responseRecorder := httptest.NewRecorder()
handler.ServeHTTP(contracts.WrapResponseWriter(responseRecorder), req)

assert.Equal(t, responseRecorder.Code, http.StatusOK)

Check failure on line 62 in internal/handler/mock/serve_fake_content_test.go

View workflow job for this annotation

GitHub Actions / Lint, Build and Test

expected-actual: need to reverse actual and expected values (testifylint)
actual := responseRecorder.Body.String()
assert.Equal(t, "{\"hello\":\"At esse ea.\",\"world\":\"Sint ut culpa.\"}\n", actual)
})

t.Run("invalid seed", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/hello?$__uncors__seed=invalid", nil)

responseRecorder := httptest.NewRecorder()
handler.ServeHTTP(contracts.WrapResponseWriter(responseRecorder), req)

assert.Equal(t, http.StatusInternalServerError, responseRecorder.Code)
actual := responseRecorder.Body.String()
assert.Contains(t, actual, "invalid $__uncors__seed parameter")
})
}

0 comments on commit a3eac92

Please sign in to comment.