-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
97 lines (77 loc) · 1.96 KB
/
Copy pathexample_test.go
File metadata and controls
97 lines (77 loc) · 1.96 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package httpx_test
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"github.com/aatuh/api-toolkit/v4/httpx"
)
func ExampleWriteJSON() {
rec := httptest.NewRecorder()
httpx.WriteJSON(rec, http.StatusAccepted, map[string]string{"status": "ok"})
fmt.Println(rec.Code)
fmt.Println(strings.TrimSpace(rec.Body.String()))
// Output:
// 202
// {"status":"ok"}
}
func ExampleWriteJSONChecked() {
rec := httptest.NewRecorder()
err := httpx.WriteJSONChecked(rec, http.StatusCreated, map[string]string{"status": "created"})
fmt.Println(err)
fmt.Println(rec.Code)
// Output:
// <nil>
// 201
}
func ExampleResponseWriteError() {
err := &httpx.ResponseWriteError{Stage: httpx.ResponseWriteStageBody}
fmt.Println(err)
// Output:
// http response body failed
}
func ExampleResponseWriteError_Unwrap() {
cause := errors.New("transport write failed")
err := &httpx.ResponseWriteError{Stage: httpx.ResponseWriteStageBody, Err: cause}
fmt.Println(errors.Is(err, cause))
fmt.Println(errors.Is(err.Unwrap(), cause))
// Output:
// true
// true
}
func ExampleResponseWriteStage() {
stages := []httpx.ResponseWriteStage{
httpx.ResponseWriteStageEncode,
httpx.ResponseWriteStageHeader,
httpx.ResponseWriteStageBody,
}
fmt.Println(stages)
// Output:
// [encode header body]
}
func ExampleWriteProblem() {
rec := httptest.NewRecorder()
httpx.WriteProblem(rec, http.StatusBadRequest, httpx.Problem{
Type: httpx.DefaultTypeURI(httpx.TypeValidation),
Title: http.StatusText(http.StatusBadRequest),
Detail: "validation failed",
})
fmt.Println(rec.Code)
fmt.Println(rec.Header().Get("Content-Type"))
// Output:
// 400
// application/problem+json
}
func ExampleWriteProblemChecked() {
rec := httptest.NewRecorder()
err := httpx.WriteProblemChecked(rec, http.StatusBadRequest, httpx.Problem{
Title: http.StatusText(http.StatusBadRequest),
Detail: "validation failed",
})
fmt.Println(err)
fmt.Println(rec.Code)
// Output:
// <nil>
// 400
}