Skip to content

Commit e668582

Browse files
authored
fix(exp): support text/plain content type with mockutil (#771)
1 parent aa05bc1 commit e668582

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

hcloud/exp/mockutil/http.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Request struct {
2525
Status int
2626
JSON any
2727
JSONRaw string
28+
TextRaw string
2829
}
2930

3031
// Handler is using a [Server] to mock http requests provided by the user.
@@ -123,6 +124,13 @@ func (m *Server) handler(w http.ResponseWriter, r *http.Request) {
123124
if err != nil {
124125
m.t.Fatal(err)
125126
}
127+
case expected.TextRaw != "":
128+
w.Header().Set("Content-Type", "text/plain")
129+
w.WriteHeader(expected.Status)
130+
_, err := w.Write([]byte(expected.TextRaw))
131+
if err != nil {
132+
m.t.Fatal(err)
133+
}
126134
default:
127135
w.WriteHeader(expected.Status)
128136
}

hcloud/exp/mockutil/http_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ func TestHandler(t *testing.T) {
3939
},
4040
Status: 200,
4141
},
42+
{
43+
Method: "GET", Path: "/",
44+
Status: 200,
45+
TextRaw: "hello",
46+
},
4247
})
4348

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

72-
// Extra request 5
77+
// Request 5
78+
resp, err = http.Get(server.URL)
79+
require.NoError(t, err)
80+
assert.Equal(t, 200, resp.StatusCode)
81+
assert.Equal(t, "text/plain", resp.Header.Get("Content-Type"))
82+
assert.Equal(t, "hello", readBody(t, resp))
83+
84+
// Extra request 6
7385
server.Expect([]Request{
7486
{Method: "GET", Path: "/", Status: 200},
7587
})

0 commit comments

Comments
 (0)