-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
66 lines (56 loc) · 1.35 KB
/
example_test.go
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
//go:build linux || darwin
package spectest_test
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"testing"
"github.com/nao1215/spectest"
)
func TestExample(t *testing.T) {
imageFile, err := os.Open(filepath.Clean(filepath.Join("testdata", "sample.png")))
if err != nil {
panic(err)
}
defer imageFile.Close() //nolint
imageInfo, err := imageFile.Stat()
if err != nil {
panic(err)
}
body, err := io.ReadAll(imageFile)
if err != nil {
panic(err)
}
handler := http.NewServeMux()
handler.HandleFunc("/image", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Content-Length", fmt.Sprint(imageInfo.Size()))
_, err = io.Copy(w, bytes.NewReader(body))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
})
spectest.New().
CustomReportName("markdow_report").
Report(spectest.SequenceReport(spectest.ReportFormatterConfig{
Path: "doc",
Kind: spectest.ReportKindMarkdown,
})).
Handler(handler).
Get("/image").
Expect(t).
Body(string(body)).
Header("Content-Type", "image/png").
Header("Content-Length", fmt.Sprint(imageInfo.Size())).
Status(http.StatusOK).
End()
}