-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraw_test.go
More file actions
71 lines (56 loc) · 1.7 KB
/
raw_test.go
File metadata and controls
71 lines (56 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package api_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/bjaus/api"
)
func TestRaw_handler(t *testing.T) {
t.Parallel()
r := api.New()
api.Raw(r, http.MethodGet, "/custom", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("custom response"))
require.NoError(t, err)
}, api.OperationInfo{
Summary: "Custom endpoint",
Description: "A fully custom endpoint",
Tags: []string{"custom"},
})
srv := httptest.NewServer(r)
defer srv.Close()
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL+"/custom", nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "text/plain", resp.Header.Get("Content-Type"))
// Verify it shows in the spec.
spec := r.Spec()
op, ok := spec.Paths["/custom"]["get"]
require.True(t, ok)
assert.Equal(t, "Custom endpoint", op.Summary)
assert.Contains(t, op.Tags, "custom")
}
func TestRawRequest_embedding(t *testing.T) {
t.Parallel()
// RawRequest should be embeddable and have a Request field.
var rr api.RawRequest
assert.Nil(t, rr.Request)
}
func TestOperationInfo_fields(t *testing.T) {
t.Parallel()
info := api.OperationInfo{
Summary: "summary",
Description: "desc",
Tags: []string{"a", "b"},
}
assert.Equal(t, "summary", info.Summary)
assert.Equal(t, "desc", info.Description)
assert.Equal(t, []string{"a", "b"}, info.Tags)
}