-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
45 lines (43 loc) · 926 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
package nktest_test
import (
"bytes"
"context"
"fmt"
"io"
"log"
"net/http"
"strconv"
)
func Example() {
// create context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
urlstr := "http://127.0.0.1:7350/healthcheck"
req, err := http.NewRequestWithContext(ctx, "GET", urlstr, nil)
if err != nil {
log.Fatal(err)
}
// create client and execute request
cl := &http.Client{
Transport: &http.Transport{
DisableCompression: true,
},
}
res, err := cl.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
// check response
if res.StatusCode != http.StatusOK {
log.Fatalf("status %d != 200", res.StatusCode)
}
// read response
buf, err := io.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println("healthcheck:", strconv.Itoa(res.StatusCode), http.StatusText(res.StatusCode), string(bytes.TrimSpace(buf)))
// Output:
// healthcheck: 200 OK {}
}