Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Nov 20, 2024
1 parent d63f1ab commit e35383f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/server/http_upstream_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (i *invoker) Invoke(ctx context.Context, rw http.ResponseWriter, r *http.Re
parser, err := protojson.NewRequestParser(r, pathNames, upstream.resovler)
if err != nil {
i.L(ctx).Error("parse request", "error", err)
rw.Write([]byte(err.Error()))

Check failure on line 24 in internal/server/http_upstream_invoker.go

View workflow job for this annotation

GitHub Actions / build (stable)

Error return value of `rw.Write` is not checked (errcheck)
return
}

Expand Down
29 changes: 29 additions & 0 deletions test/integration/http_grpc_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package integration

import (
"bytes"
"context"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -92,4 +93,32 @@ func TestHTTP2Grpc(t *testing.T) {
assert.Equal(t, `{"code":2,"message":"error","details":[]}`, rec.Body.String())
}, kod.WithFakes(kod.Fake[config.Config](mockConfig)), kod.WithOpenTelemetryDisabled())
})

t.Run("http body", func(t *testing.T) {
kod.RunTest(t, func(ctx context.Context, up server.Upstream) {
router := http.NewServeMux()
up.Register(ctx, router)
rec := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/say/sam", bytes.NewBufferString("{\"name\":\"bob\"}"))
req.Header.Set("Content-Type", "application/json")

router.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, `{"message":"Hello sam"}`, rec.Body.String())
}, kod.WithFakes(kod.Fake[config.Config](mockConfig)), kod.WithOpenTelemetryDisabled())
})

t.Run("invalid http body", func(t *testing.T) {
kod.RunTest(t, func(ctx context.Context, up server.Upstream) {
router := http.NewServeMux()
up.Register(ctx, router)
rec := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/say/sam", bytes.NewBufferString("{invalid data}"))
req.Header.Set("Content-Type", "application/json")

router.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, `invalid character 'i' looking for beginning of object key string`, rec.Body.String())
}, kod.WithFakes(kod.Fake[config.Config](mockConfig)), kod.WithOpenTelemetryDisabled())
})
}

0 comments on commit e35383f

Please sign in to comment.