-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_test.go
115 lines (104 loc) · 2.71 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//go:build go1.7
// +build go1.7
package rest_test
import (
"bufio"
"bytes"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"github.com/kevinburke/rest"
"github.com/kevinburke/rest/restclient"
)
func ExampleRegisterHandler() {
rest.RegisterHandler(500, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := rest.CtxErr(r)
fmt.Println("Server error:", err)
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(500)
w.Write([]byte("<html><body>Server Error</body></html>"))
}))
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
rest.ServerError(w, req, errors.New("Something bad happened"))
// Output: Server error: Something bad happened
}
func ExampleClient() {
client := restclient.New("jobs", "secretpassword", "http://ipinfo.io")
req, _ := client.NewRequest("GET", "/json", nil)
type resp struct {
City string `json:"city"`
Ip string `json:"ip"`
}
var r resp
client.Do(req, &r)
fmt.Println(r.Ip)
}
func ExampleNewClient() {
// Deprecated: use restclient.New instead.
client := rest.NewClient("jobs", "secretpassword", "http://ipinfo.io")
// Use restclient.Client.NewRequestWithContext instead.
req, _ := client.NewRequest("GET", "/json", nil)
type resp struct {
City string `json:"city"`
Ip string `json:"ip"`
}
var r resp
client.Do(req, &r)
fmt.Println(r.Ip)
}
func ExampleClient_NewRequest() {
client := rest.NewClient("jobs", "secretpassword", "http://ipinfo.io")
req, _ := client.NewRequest("GET", "/json", nil)
type resp struct {
City string `json:"city"`
Ip string `json:"ip"`
}
var r resp
client.Do(req, &r)
fmt.Println(r.Ip)
}
func ExampleClient_Do() {
client := rest.NewClient("jobs", "secretpassword", "http://ipinfo.io")
req, _ := client.NewRequest("GET", "/json", nil)
type resp struct {
City string `json:"city"`
Ip string `json:"ip"`
}
var r resp
client.Do(req, &r)
fmt.Println(r.Ip)
}
func ExampleTransport() {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
}))
defer server.Close()
b := new(bytes.Buffer)
client := http.Client{
Transport: &rest.Transport{Debug: true, Output: b},
}
req, _ := http.NewRequest("GET", server.URL+"/bar", nil)
client.Do(req)
// Dump the HTTP request from the buffer, but skip the lines that change.
scanner := bufio.NewScanner(b)
for scanner.Scan() {
text := scanner.Text()
if strings.HasPrefix(text, "Host:") || strings.HasPrefix(text, "Date:") {
continue
}
fmt.Println(text)
}
// Output:
// GET /bar HTTP/1.1
// User-Agent: Go-http-client/1.1
// Accept-Encoding: gzip
//
// HTTP/1.1 200 OK
// Content-Length: 11
// Content-Type: text/plain; charset=utf-8
//
// Hello World
}