-
Notifications
You must be signed in to change notification settings - Fork 2
/
examples_test.go
109 lines (72 loc) · 1.71 KB
/
examples_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
package rest_test
import (
"encoding/json"
"errors"
"github.com/edermanoel94/rest-go"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
)
func ExampleMarshalled() {
product := struct {
Name string `json:"name"`
Price float32 `json:"price"`
}{
Name: "Smart TV",
Price: 150.20,
}
recorder := httptest.NewRecorder()
rest.Marshalled(recorder, &product, http.StatusOK)
result := recorder.Result()
defer result.Body.Close()
_, _ = io.Copy(os.Stdout, result.Body)
// Output: {"name":"Smart TV","price":150.2}
}
func ExampleContent() {
product := struct {
Name string `json:"name"`
Price float32 `json:"price"`
}{
Name: "Smart TV",
Price: 100.00,
}
bytes, _ := json.Marshal(&product)
recorder := httptest.NewRecorder()
rest.Response(recorder, bytes, http.StatusOK)
result := recorder.Result()
defer result.Body.Close()
_, _ = io.Copy(os.Stdout, result.Body)
// Output: {"name":"Smart TV","price":100}
}
func ExampleError() {
err := errors.New("cannot create product")
recorder := httptest.NewRecorder()
rest.Error(recorder, err, http.StatusOK)
result := recorder.Result()
defer result.Body.Close()
_, _ = io.Copy(os.Stdout, result.Body)
// Output: {"message":"cannot create product"}
}
func ExampleGetBody() {
product := struct {
Name string `json:"name"`
}{}
readerCloser := ioutil.NopCloser(strings.NewReader("{\"name\": \"eder\"}"))
err := rest.GetBody(readerCloser, &product)
if err != nil {
// do stuff with error
}
// Output:
}
func ExampleCheckPathVariables() {
params := make(map[string]string)
params["id"] = "21321423"
err := rest.CheckPathVariables(params, "id")
if err != nil {
// do stuff with error
}
// Output:
}