| 1 | // Copyright 2020 The Go Authors. All rights reserved. |
|---|---|
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package jsonrpc2_test |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "encoding/json" |
| 10 | "fmt" |
| 11 | "testing" |
| 12 | |
| 13 | "golang.org/x/tools/internal/jsonrpc2" |
| 14 | ) |
| 15 | |
| 16 | var wireIDTestData = []struct { |
| 17 | name string |
| 18 | id jsonrpc2.ID |
| 19 | encoded []byte |
| 20 | plain string |
| 21 | quoted string |
| 22 | }{ |
| 23 | { |
| 24 | name: `empty`, |
| 25 | encoded: []byte(`0`), |
| 26 | plain: `0`, |
| 27 | quoted: `#0`, |
| 28 | }, { |
| 29 | name: `number`, |
| 30 | id: jsonrpc2.NewIntID(43), |
| 31 | encoded: []byte(`43`), |
| 32 | plain: `43`, |
| 33 | quoted: `#43`, |
| 34 | }, { |
| 35 | name: `string`, |
| 36 | id: jsonrpc2.NewStringID("life"), |
| 37 | encoded: []byte(`"life"`), |
| 38 | plain: `life`, |
| 39 | quoted: `"life"`, |
| 40 | }, |
| 41 | } |
| 42 | |
| 43 | func TestIDFormat(t *testing.T) { |
| 44 | for _, test := range wireIDTestData { |
| 45 | t.Run(test.name, func(t *testing.T) { |
| 46 | if got := fmt.Sprint(test.id); got != test.plain { |
| 47 | t.Errorf("got %s expected %s", got, test.plain) |
| 48 | } |
| 49 | if got := fmt.Sprintf("%q", test.id); got != test.quoted { |
| 50 | t.Errorf("got %s want %s", got, test.quoted) |
| 51 | } |
| 52 | }) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestIDEncode(t *testing.T) { |
| 57 | for _, test := range wireIDTestData { |
| 58 | t.Run(test.name, func(t *testing.T) { |
| 59 | data, err := json.Marshal(&test.id) |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | checkJSON(t, data, test.encoded) |
| 64 | }) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestIDDecode(t *testing.T) { |
| 69 | for _, test := range wireIDTestData { |
| 70 | t.Run(test.name, func(t *testing.T) { |
| 71 | var got *jsonrpc2.ID |
| 72 | if err := json.Unmarshal(test.encoded, &got); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | if got == nil { |
| 76 | t.Errorf("got nil want %s", test.id) |
| 77 | } else if *got != test.id { |
| 78 | t.Errorf("got %s want %s", got, test.id) |
| 79 | } |
| 80 | }) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestErrorEncode(t *testing.T) { |
| 85 | b, err := json.Marshal(jsonrpc2.NewError(0, "")) |
| 86 | if err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | checkJSON(t, b, []byte(`{ |
| 90 | "code": 0, |
| 91 | "message": "" |
| 92 | }`)) |
| 93 | } |
| 94 | |
| 95 | func TestErrorResponse(t *testing.T) { |
| 96 | // originally reported in #39719, this checks that result is not present if |
| 97 | // it is an error response |
| 98 | r, _ := jsonrpc2.NewResponse(jsonrpc2.NewIntID(3), nil, fmt.Errorf("computing fix edits")) |
| 99 | data, err := json.Marshal(r) |
| 100 | if err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | checkJSON(t, data, []byte(`{ |
| 104 | "jsonrpc":"2.0", |
| 105 | "error":{ |
| 106 | "code":0, |
| 107 | "message":"computing fix edits" |
| 108 | }, |
| 109 | "id":3 |
| 110 | }`)) |
| 111 | } |
| 112 | |
| 113 | func checkJSON(t *testing.T, got, want []byte) { |
| 114 | // compare the compact form, to allow for formatting differences |
| 115 | g := &bytes.Buffer{} |
| 116 | if err := json.Compact(g, []byte(got)); err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | w := &bytes.Buffer{} |
| 120 | if err := json.Compact(w, []byte(want)); err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | if g.String() != w.String() { |
| 124 | t.Fatalf("Got:\n%s\nWant:\n%s", g, w) |
| 125 | } |
| 126 | } |
| 127 |
Members