Skip to content

Commit

Permalink
fix: http test lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Filippo Trotter authored and Filippo Trotter committed Dec 5, 2024
1 parent 2a6d3c9 commit d14f621
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions cmd/httpserver/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ Then print the data
`
err := os.MkdirAll(filepath.Dir(mockSlangFile), os.ModePerm)
require.NoError(t, err)
err = os.WriteFile(mockSlangFile, []byte(mockSlangContent), 0644)
err = os.WriteFile(mockSlangFile, []byte(mockSlangContent), 0600)
require.NoError(t, err)
defer os.RemoveAll("./testdata/slang")
defer func() {
if err := os.RemoveAll("./testdata"); err != nil {
t.Errorf("Failed to remove test directory: %v", err)
}
}()

ctx := context.Background()

Expand All @@ -41,22 +45,27 @@ Then print the data
require.NoError(t, err)
require.NotNil(t, muxRouter)

// Test if the router serves OpenAPI documentation
t.Run("correctly exposes OpenAPI documentation", func(t *testing.T) {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, swagger.DefaultJSONDocumentationPath, nil)

muxRouter.ServeHTTP(w, req)

require.Equal(t, http.StatusOK, w.Result().StatusCode)
require.Equal(t, "application/json", w.Result().Header.Get("Content-Type"))

body, err := io.ReadAll(w.Result().Body)
body := w.Result().Body // //nolint:bodyclose
defer func() {
if err := body.Close(); err != nil {
t.Errorf("Failed to close body: %v", err)
}
}()

// These lines do not require closing since they just access StatusCode and Headers
require.Equal(t, http.StatusOK, w.Result().StatusCode) //nolint:bodyclose
require.Equal(t, "application/json", w.Result().Header.Get("Content-Type")) //nolint:bodyclose

bodycontent, err := io.ReadAll(body)
require.NoError(t, err)

expected := `{"info":{"title":"TestBinary","version":"1.0.0"},"openapi":"3.0.0","paths":{"/example":{"get":{"description":"\u003cpre\u003eRule unknown ignore\nGiven I have a 'string' named 'test'\nThen print the data\n\u003c/pre\u003e","parameters":[{"description":"The test","in":"query","name":"test","schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"properties":{"output":{"items":{"type":"string"},"type":"array"}},"required":["output"],"type":"object"}}},"description":"The slangroom execution output, splitted by newline"},"500":{"content":{"application/json":{"schema":{"properties":{"message":{"items":{"type":"string"},"type":"array"}},"required":["message"],"type":"object"}}},"description":"Slangroom execution error"}},"tags":["📑 Zencodes"]},"post":{"description":"\u003cpre\u003eRule unknown ignore\nGiven I have a 'string' named 'test'\nThen print the data\n\u003c/pre\u003e","requestBody":{"content":{"application/json":{"schema":{"properties":{"test":{"type":"string"}},"required":["test"],"type":"object"}}}},"responses":{"200":{"content":{"application/json":{"schema":{"properties":{"output":{"items":{"type":"string"},"type":"array"}},"required":["output"],"type":"object"}}},"description":"The slangroom execution output, split by newline"},"500":{"content":{"application/json":{"schema":{"properties":{"message":{"items":{"type":"string"},"type":"array"}},"required":["message"],"type":"object"}}},"description":"Slangroom execution error"}},"tags":["📑 Zencodes"]}}},"tags":[{"description":"Endpoints generated over the Zencode smart contracts","name":"📑 Zencodes"}]}`

require.JSONEq(t, string(expected), string(body), "actual json data: %s", body)
require.JSONEq(t, expected, string(bodycontent), "actual json data: %s", body)
})

// Test POST route execution
Expand All @@ -65,16 +74,30 @@ Then print the data
req := httptest.NewRequest(http.MethodPost, "/example", nil) // Mocked route

muxRouter.ServeHTTP(w, req)

require.Equal(t, http.StatusOK, w.Result().StatusCode)
body := w.Result().Body //nolint:bodyclose
defer func() {
if err := body.Close(); err != nil {
t.Errorf("Failed to close body: %v", err)
}
}()

require.Equal(t, http.StatusOK, w.Result().StatusCode) //nolint:bodyclose
require.Equal(t, "OK", w.Body.String())
})

// Test GET route execution
t.Run("correctly handles GET route", func(t *testing.T) {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/example", nil) // Mocked route

muxRouter.ServeHTTP(w, req)

require.Equal(t, http.StatusOK, w.Result().StatusCode)
body := w.Result().Body //nolint:bodyclose
defer func() {
if err := body.Close(); err != nil {
t.Errorf("Failed to close body: %v", err)
}
}()

require.Equal(t, http.StatusOK, w.Result().StatusCode) //nolint:bodyclose
})
}

0 comments on commit d14f621

Please sign in to comment.