Debian Codegraph @

decoding_test.go

raw symbols SCIP
1 package errors
2
3 import (
4 "reflect"
5 "testing"
6 )
7
8 func Test_UnmarshalJSON(t *testing.T) {
9 tests := []struct {
10 name string
11 args []byte
12 want []Error
13 wantErr bool
14 }{
15 {
16 name: "nil",
17 args: nil,
18 want: nil,
19 wantErr: false,
20 },
21 {
22 name: "empty",
23 args: []byte{'{', '}'},
24 want: []Error(nil),
25 wantErr: true,
26 },
27 {
28 name: "no errors",
29 args: []byte(`{"errors":[]}`),
30 want: []Error{},
31 wantErr: false,
32 },
33 {
34 name: "rl-example",
35 args: []byte(`{"@context":"https://fedbox.git/ns#errors","errors":[{"status":403,"message":"Error processing OAuth2 request: The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed.","trace":[{"file":"/home/habarnam/workspace/go-ap/errors/errors.go","line":79,"calee":"github.com/go-ap/errors.wrap(0x114baa0, 0xc0005075c0, 0x1041334, 0x23, 0xc0003e5078, 0x1, 0x1, 0x0, 0x0, 0x0, ...)"},{"file":"/home/habarnam/workspace/go-ap/errors/errors.go","line":42,"calee":"github.com/go-ap/errors.Annotatef(0x114baa0, 0xc0005075c0, 0x1041334, 0x23, 0xc0003e5078, 0x1, 0x1, 0x0)"},{"file":"/home/habarnam/workspace/go-ap/errors/http.go","line":54,"calee":"github.com/go-ap/errors.wrapErr(0x114baa0, 0xc0005075c0, 0x1041334, 0x23, 0xc0003e5078, 0x1, 0x1, 0x0, 0x0, 0x0, ...)"},{"file":"/home/habarnam/workspace/go-ap/errors/http.go","line":130,"calee":"github.com/go-ap/errors.NewForbidden(0x114baa0, 0xc0005075c0, 0x1041334, 0x23, 0xc0003e5078, 0x1, 0x1, 0x0)"},{"file":"/home/habarnam/workspace/fedbox/app/oauth.go","line":163,"calee":"github.com/go-ap/fedbox/app.annotatedRsError(0x193, 0x114baa0, 0xc0005075c0, 0x1041334, 0x23, 0xc0003e5078, 0x1, 0x1, 0x0, 0x0)"},{"file":"/home/habarnam/workspace/fedbox/app/oauth.go","line":177,"calee":"github.com/go-ap/fedbox/app.redirectOrOutput(0xc0000a0120, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cc00)"},{"file":"/home/habarnam/workspace/fedbox/app/oauth.go","line":83,"calee":"github.com/go-ap/fedbox/app.(*oauthHandler).Authorize(0xc00026dec0, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cc00)"},{"file":"/usr/lib/go/src/net/http/server.go","line":2012,"calee":"net/http.HandlerFunc.ServeHTTP(0xc000206a50, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cc00)"},{"file":"/home/habarnam/.local/share/go/pkg/mod/github.com/go-chi/[email protected]+incompatible/mux.go","line":431,"calee":"github.com/go-chi/chi.(*Mux).routeHTTP(0xc0002012c0, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cc00)"},{"file":"/usr/lib/go/src/net/http/server.go","line":2012,"calee":"net/http.HandlerFunc.ServeHTTP(0xc000206a60, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cc00)"},{"file":"/home/habarnam/.local/share/go/pkg/mod/github.com/go-chi/[email protected]+incompatible/mux.go","line":70,"calee":"github.com/go-chi/chi.(*Mux).ServeHTTP(0xc0002012c0, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cc00)"},{"file":"/home/habarnam/.local/share/go/pkg/mod/github.com/go-chi/[email protected]+incompatible/mux.go","line":298,"calee":"github.com/go-chi/chi.(*Mux).Mount.func1(0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cc00)"},{"file":"/usr/lib/go/src/net/http/server.go","line":2012,"calee":"net/http.HandlerFunc.ServeHTTP(0xc000209ec0, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cc00)"},{"file":"/home/habarnam/.local/share/go/pkg/mod/github.com/go-chi/[email protected]+incompatible/mux.go","line":431,"calee":"github.com/go-chi/chi.(*Mux).routeHTTP(0xc000200f60, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cc00)"},{"file":"/usr/lib/go/src/net/http/server.go","line":2012,"calee":"net/http.HandlerFunc.ServeHTTP(0xc000206890, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cc00)"},{"file":"/home/habarnam/workspace/fedbox/app/middleware.go","line":61,"calee":"github.com/go-ap/fedbox/app.ActorFromAuthHeader.func1.1(0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cc00)"},{"file":"/usr/lib/go/src/net/http/server.go","line":2012,"calee":"net/http.HandlerFunc.ServeHTTP(0xc000211940, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cb00)"},{"file":"/home/habarnam/.local/share/go/pkg/mod/github.com/go-chi/[email protected]+incompatible/middleware/get_head.go","line":37,"calee":"github.com/go-chi/chi/middleware.GetHead.func1(0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cb00)"},{"file":"/usr/lib/go/src/net/http/server.go","line":2012,"calee":"net/http.HandlerFunc.ServeHTTP(0xc000209980, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cb00)"},{"file":"/home/habarnam/.local/share/go/pkg/mod/github.com/go-chi/[email protected]+incompatible/middleware/realip.go","line":34,"calee":"github.com/go-chi/chi/middleware.RealIP.func1(0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cb00)"},{"file":"/usr/lib/go/src/net/http/server.go","line":2012,"calee":"net/http.HandlerFunc.ServeHTTP(0xc0002099a0, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cb00)"},{"file":"/home/habarnam/.local/share/go/pkg/mod/github.com/go-chi/[email protected]+incompatible/mux.go","line":70,"calee":"github.com/go-chi/chi.(*Mux).ServeHTTP(0xc000200f60, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cb00)"},{"file":"/home/habarnam/.local/share/go/pkg/mod/github.com/go-chi/[email protected]+incompatible/mux.go","line":298,"calee":"github.com/go-chi/chi.(*Mux).Mount.func1(0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cb00)"},{"file":"/usr/lib/go/src/net/http/server.go","line":2012,"calee":"net/http.HandlerFunc.ServeHTTP(0xc000209f80, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cb00)"},{"file":"/home/habarnam/.local/share/go/pkg/mod/github.com/go-chi/[email protected]+incompatible/mux.go","line":431,"calee":"github.com/go-chi/chi.(*Mux).routeHTTP(0xc0002004e0, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cb00)"},{"file":"/usr/lib/go/src/net/http/server.go","line":2012,"calee":"net/http.HandlerFunc.ServeHTTP(0xc000206b80, 0x7fb8c9b0baa8, 0xc00009a6c0, 0xc00021cb00)"},{"file":"/home/habarnam/.local/share/go/pkg/mod/github.com/go-chi/[email protected]+incompatible/middleware/logger.go","line":46,"calee":"github.com/go-chi/chi/middleware.RequestLogger.func1.1(0x1158d00, 0xc000010028, 0xc00021ca00)"},{"file":"/usr/lib/go/src/net/http/server.go","line":2012,"calee":"net/http.HandlerFunc.ServeHTTP(0xc000274ae0, 0x1158d00, 0xc000010028, 0xc00021ca00)"},{"file":"/home/habarnam/.local/share/go/pkg/mod/github.com/go-chi/[email protected]+incompatible/middleware/request_id.go","line":76,"calee":"github.com/go-chi/chi/middleware.RequestID.func1(0x1158d00, 0xc000010028, 0xc00021c900)"},{"file":"/usr/lib/go/src/net/http/server.go","line":2012,"calee":"net/http.HandlerFunc.ServeHTTP(0xc000209fa0, 0x1158d00, 0xc000010028, 0xc00021c900)"},{"file":"/home/habarnam/workspace/fedbox/app/middleware.go","line":21,"calee":"github.com/go-ap/fedbox/app.Repo.func1.1(0x1158d00, 0xc000010028, 0xc00021c800)"},{"file":"/usr/lib/go/src/net/http/server.go","line":2012,"calee":"net/http.HandlerFunc.ServeHTTP(0xc000274b10, 0x1158d00, 0xc000010028, 0xc00021c800)"},{"file":"/home/habarnam/.local/share/go/pkg/mod/github.com/go-chi/[email protected]+incompatible/mux.go","line":86,"calee":"github.com/go-chi/chi.(*Mux).ServeHTTP(0xc0002004e0, 0x1158d00, 0xc000010028, 0xc00021c800)"},{"file":"/usr/lib/go/src/net/http/server.go","line":2807,"calee":"net/http.serverHandler.ServeHTTP(0xc0002820e0, 0x1158d00, 0xc000010028, 0xc000524100)"},{"file":"/usr/lib/go/src/net/http/server.go","line":3381,"calee":"net/http.initALPNRequest.ServeHTTP(0x115d000, 0xc000090630, 0xc000080380, 0xc0002820e0, 0x1158d00, 0xc000010028, 0xc000524100)"},{"file":"/usr/lib/go/src/net/http/h2_bundle.go","line":5720,"calee":"net/http.(*http2serverConn).runHandler(0xc000582600, 0xc000010028, 0xc000524100, 0xc00029e000)"},{"file":"/usr/lib/go/src/net/http/h2_bundle.go","line":5454,"calee":"created by net/http.(*http2serverConn).processHeaders"}],"location":"/home/habarnam/workspace/go-ap/errors/http.go:54"},{"status":500,"message":"urls don't validate: https://brutalinks.git/auth/fedbox/callback / http://brutalinks.git/auth/fedbox/callback"}]}`),
36 want: []Error{
37 &forbidden{
38 Err: Err{
39 m: "Error processing OAuth2 request: The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed.",
40 },
41 },
42 &Err{
43 m: "urls don't validate: https://brutalinks.git/auth/fedbox/callback / http://brutalinks.git/auth/fedbox/callback",
44 },
45 },
46 wantErr: false,
47 },
48 }
49 for _, tt := range tests {
50 t.Run(tt.name, func(t *testing.T) {
51 got, err := UnmarshalJSON(tt.args)
52 if (err != nil) != tt.wantErr {
53 t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
54 return
55 }
56 if len(tt.want) != len(got) {
57 t.Errorf("UnmarshalJSON() different lengths got = %d, want %d", len(got), len(tt.want))
58 }
59 for i := range tt.want {
60 g := got[i]
61 w := tt.want[i]
62 switch ww := w.(type) {
63 case *badRequest:
64 assertEqual(t, g.(*badRequest).Err, ww.Err)
65 case *unauthorized:
66 assertEqual(t, g.(*unauthorized).Err, ww.Err)
67 case *forbidden:
68 assertEqual(t, g.(*forbidden).Err, ww.Err)
69 case *notFound:
70 assertEqual(t, g.(*notFound).Err, ww.Err)
71 case *methodNotAllowed:
72 assertEqual(t, g.(*methodNotAllowed).Err, ww.Err)
73 case *notValid:
74 assertEqual(t, g.(*notValid).Err, ww.Err)
75 case *notImplemented:
76 assertEqual(t, g.(*notImplemented).Err, ww.Err)
77 case *badGateway:
78 assertEqual(t, g.(*badGateway).Err, ww.Err)
79 case *notSupportedVersion:
80 assertEqual(t, g.(*notSupportedVersion).Err, ww.Err)
81 case *Err:
82 assertEqual(t, *g.(*Err), *ww)
83 }
84 }
85 })
86 }
87 }
88
89 func assertEqual(t *testing.T, g Err, w Err) {
90 if w.m != g.m {
91 t.Errorf("UnmarshalJSON() Err.m got = %s, want %s", g.m, w.m)
92 }
93 if w.c != g.c {
94 t.Errorf("UnmarshalJSON() Err.c got = %s, want %s", g.c, w.c)
95 }
96 if !reflect.DeepEqual(w.t.StackTrace(), g.t.StackTrace()) {
97 t.Errorf("UnmarshalJSON() Err.t got = %v, want %v", g.t, w.t)
98 }
99 }