Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions hcloud/exp/mockutil/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Request struct {
Status int
JSON any
JSONRaw string
TextRaw string
}

// Handler is using a [Server] to mock http requests provided by the user.
Expand Down Expand Up @@ -123,6 +124,13 @@ func (m *Server) handler(w http.ResponseWriter, r *http.Request) {
if err != nil {
m.t.Fatal(err)
}
case expected.TextRaw != "":
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(expected.Status)
_, err := w.Write([]byte(expected.TextRaw))
if err != nil {
m.t.Fatal(err)
}
default:
w.WriteHeader(expected.Status)
}
Expand Down
14 changes: 13 additions & 1 deletion hcloud/exp/mockutil/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func TestHandler(t *testing.T) {
},
Status: 200,
},
{
Method: "GET", Path: "/",
Status: 200,
TextRaw: "hello",
},
})

// Request 1
Expand Down Expand Up @@ -69,7 +74,14 @@ func TestHandler(t *testing.T) {
assert.Empty(t, resp.Header.Get("Content-Type"))
assert.Empty(t, readBody(t, resp))

// Extra request 5
// Request 5
resp, err = http.Get(server.URL)
require.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, "text/plain", resp.Header.Get("Content-Type"))
assert.Equal(t, "hello", readBody(t, resp))

// Extra request 6
server.Expect([]Request{
{Method: "GET", Path: "/", Status: 200},
})
Expand Down