-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_test.go
49 lines (43 loc) · 904 Bytes
/
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
package hx_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"github.com/izumin5210/hx"
)
func ExampleGet() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && r.URL.Path == "/echo":
err := json.NewEncoder(w).Encode(map[string]string{
"message": r.URL.Query().Get("message"),
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer ts.Close()
var out struct {
Message string `json:"message"`
}
ctx := context.Background()
err := hx.Get(
ctx,
ts.URL+"/echo",
hx.Query("message", "It Works!"),
hx.WhenSuccess(hx.AsJSON(&out)),
hx.WhenFailure(hx.AsError()),
)
if err != nil {
// Handle errors...
}
fmt.Println(out.Message)
// Output:
// It Works!
}