MCPcopy Create free account
hub / github.com/dolanor/rip / TestHandleResourceWithPath

Function TestHandleResourceWithPath

handler_test.go:19–286  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

17)
18
19func TestHandleResourceWithPath(t *testing.T) {
20 up := newUserProvider()
21 ro := NewRouteOptions().
22 WithCodecs(
23 json.Codec,
24 yaml.Codec,
25 msgpack.Codec,
26 xml.Codec,
27 )
28
29 mux := http.NewServeMux()
30 mux.HandleFunc(HandleEntities("/users/", up, ro))
31 s := httptest.NewServer(mux)
32
33 u := user{Name: "Jane", BirthDate: time.Date(2009, time.November, 1, 23, 0, 0, 0, time.UTC)}
34
35 c := s.Client()
36
37 availableCodecs := ro.codecs.Codecs
38 t.Log(ro.codecs)
39
40 for name, codec := range availableCodecs {
41 var b bytes.Buffer
42 err := codec.NewEncoder(&b).Encode(u)
43 panicErr(t, err)
44 t.Run(name, func(t *testing.T) {
45 if name == encoding.DefaultCodecKey {
46 // it's an internal logic, it's not a known mime type
47 t.Skip("skipping default codec key")
48 }
49 t.Run("create", func(t *testing.T) {
50 req, err := http.NewRequest(http.MethodPost, s.URL+"/users/", &b)
51 panicErr(t, err)
52 req.Header["Content-Type"] = []string{name}
53 req.Header["Accept"] = []string{name}
54
55 resp, err := c.Do(req)
56 panicErr(t, err)
57 defer resp.Body.Close()
58
59 if resp.StatusCode != http.StatusCreated {
60 b, err := io.ReadAll(resp.Body)
61 t.Fatalf("post status code is not 201: body: %v: %s", err, string(b))
62 }
63 var buf bytes.Buffer
64 _, err = buf.ReadFrom(resp.Body)
65 panicErr(t, err)
66
67 var uCreated user
68 err = codec.NewDecoder(&buf).Decode(&uCreated)
69
70 panicErr(t, err)
71 // somehow msgpack package changes the time reference (UTC to CET)
72 // so we're more graceful with the checks
73 if uCreated != u && !uCreated.BirthDate.Equal(u.BirthDate) {
74 t.Fatal("user created != from original:", uCreated, u)
75 }
76 })

Callers

nothing calls this directly

Calls 10

DecodeMethod · 0.95
newUserProviderFunction · 0.85
NewRouteOptionsFunction · 0.85
HandleEntitiesFunction · 0.85
panicErrFunction · 0.85
WithCodecsMethod · 0.80
StringMethod · 0.80
HandleFuncMethod · 0.65
EncodeMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected