MCPcopy Create free account
hub / github.com/DoNewsCode/core / ExampleResponseEncoder_EncodeResponse

Function ExampleResponseEncoder_EncodeResponse

srvhttp/example_test.go:35–60  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

33}
34
35func ExampleResponseEncoder_EncodeResponse() {
36 handler := func(writer http.ResponseWriter, request *http.Request) {
37 encoder := srvhttp.NewResponseEncoder(writer)
38 encoder.EncodeResponse(struct {
39 Foo string `json:"foo"`
40 }{
41 Foo: "bar",
42 })
43 }
44 req := httptest.NewRequest("GET", "http://example.com/foo", nil)
45 w := httptest.NewRecorder()
46 handler(w, req)
47
48 resp := w.Result()
49 defer resp.Body.Close()
50 body, _ := ioutil.ReadAll(resp.Body)
51
52 fmt.Println(resp.StatusCode)
53 fmt.Println(resp.Header.Get("Content-Type"))
54 fmt.Println(string(body))
55
56 // Output:
57 // 200
58 // application/json; charset=utf-8
59 // {"foo":"bar"}
60}
61
62func ExampleResponseEncoder_EncodeError() {
63 handler := func(writer http.ResponseWriter, request *http.Request) {

Callers

nothing calls this directly

Calls 5

EncodeResponseMethod · 0.95
NewResponseEncoderFunction · 0.92
handlerInterface · 0.85
CloseMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected