-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
38 lines (30 loc) · 764 Bytes
/
Copy pathexample_test.go
File metadata and controls
38 lines (30 loc) · 764 Bytes
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
package httpx_test
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"github.com/aatuh/api-toolkit/v3/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 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
}