OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package prpc | 5 package prpc |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "encoding/base64" | 9 "encoding/base64" |
10 "io/ioutil" | 10 "io/ioutil" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 | 49 |
50 test(mtJSON, formatJSONPB, nil) | 50 test(mtJSON, formatJSONPB, nil) |
51 test(mtJSON+"; whatever=true", formatJSONPB, nil) | 51 test(mtJSON+"; whatever=true", formatJSONPB, nil) |
52 | 52 |
53 test("x", 0, `"x" is not supported`) | 53 test("x", 0, `"x" is not supported`) |
54 test("x,y", 0, "mime: expected slash after first token") | 54 test("x,y", 0, "mime: expected slash after first token") |
55 }) | 55 }) |
56 | 56 |
57 Convey("readMessage", t, func() { | 57 Convey("readMessage", t, func() { |
58 var msg HelloRequest | 58 var msg HelloRequest |
59 » » read := func(contentType string, body []byte) error { | 59 » » read := func(contentType string, body []byte) *protocolError { |
60 req := &http.Request{ | 60 req := &http.Request{ |
61 Body: ioutil.NopCloser(bytes.NewBuffer(body)), | 61 Body: ioutil.NopCloser(bytes.NewBuffer(body)), |
62 Header: http.Header{}, | 62 Header: http.Header{}, |
63 } | 63 } |
64 req.Header.Set("Content-Type", contentType) | 64 req.Header.Set("Content-Type", contentType) |
65 return readMessage(req, &msg) | 65 return readMessage(req, &msg) |
66 } | 66 } |
67 | 67 |
68 testLucy := func(contentType string, body []byte) { | 68 testLucy := func(contentType string, body []byte) { |
69 err := read(contentType, body) | 69 err := read(contentType, body) |
70 So(err, ShouldBeNil) | 70 So(err, ShouldBeNil) |
71 So(msg.Name, ShouldEqual, "Lucy") | 71 So(msg.Name, ShouldEqual, "Lucy") |
72 } | 72 } |
73 | 73 |
74 Convey("binary", func() { | 74 Convey("binary", func() { |
75 testMsg := &HelloRequest{Name: "Lucy"} | 75 testMsg := &HelloRequest{Name: "Lucy"} |
76 body, err := proto.Marshal(testMsg) | 76 body, err := proto.Marshal(testMsg) |
77 So(err, ShouldBeNil) | 77 So(err, ShouldBeNil) |
78 | 78 |
79 Convey(mtPRPC, func() { | 79 Convey(mtPRPC, func() { |
80 testLucy(mtPRPC, body) | 80 testLucy(mtPRPC, body) |
81 }) | 81 }) |
82 Convey(mtPRPCBinary, func() { | 82 Convey(mtPRPCBinary, func() { |
83 testLucy(mtPRPCBinary, body) | 83 testLucy(mtPRPCBinary, body) |
84 }) | 84 }) |
85 Convey("malformed body", func() { | 85 Convey("malformed body", func() { |
86 err := read(mtPRPCBinary, []byte{0}) | 86 err := read(mtPRPCBinary, []byte{0}) |
87 So(err, ShouldNotBeNil) | 87 So(err, ShouldNotBeNil) |
88 » » » » So(ErrorStatus(err), ShouldEqual, http.StatusBad
Request) | 88 » » » » So(err.status, ShouldEqual, http.StatusBadReques
t) |
89 }) | 89 }) |
90 Convey("empty body", func() { | 90 Convey("empty body", func() { |
91 err := read(mtPRPCBinary, nil) | 91 err := read(mtPRPCBinary, nil) |
92 So(err, ShouldBeNil) | 92 So(err, ShouldBeNil) |
93 }) | 93 }) |
94 }) | 94 }) |
95 | 95 |
96 Convey("json", func() { | 96 Convey("json", func() { |
97 body := []byte(`{"name": "Lucy"}`) | 97 body := []byte(`{"name": "Lucy"}`) |
98 Convey(mtJSON, func() { | 98 Convey(mtJSON, func() { |
99 testLucy(mtJSON, body) | 99 testLucy(mtJSON, body) |
100 }) | 100 }) |
101 Convey(mtPRPCJSNOPB, func() { | 101 Convey(mtPRPCJSNOPB, func() { |
102 testLucy(mtPRPCJSNOPB, body) | 102 testLucy(mtPRPCJSNOPB, body) |
103 }) | 103 }) |
104 Convey("malformed body", func() { | 104 Convey("malformed body", func() { |
105 err := read(mtPRPCJSNOPB, []byte{0}) | 105 err := read(mtPRPCJSNOPB, []byte{0}) |
106 So(err, ShouldNotBeNil) | 106 So(err, ShouldNotBeNil) |
107 » » » » So(ErrorStatus(err), ShouldEqual, http.StatusBad
Request) | 107 » » » » So(err.status, ShouldEqual, http.StatusBadReques
t) |
108 }) | 108 }) |
109 Convey("empty body", func() { | 109 Convey("empty body", func() { |
110 err := read(mtPRPCJSNOPB, nil) | 110 err := read(mtPRPCJSNOPB, nil) |
111 So(err, ShouldBeNil) | 111 So(err, ShouldBeNil) |
112 }) | 112 }) |
113 }) | 113 }) |
114 | 114 |
115 Convey("text", func() { | 115 Convey("text", func() { |
116 Convey(mtPRPCText, func() { | 116 Convey(mtPRPCText, func() { |
117 body := []byte(`name: "Lucy"`) | 117 body := []byte(`name: "Lucy"`) |
118 testLucy(mtPRPCText, body) | 118 testLucy(mtPRPCText, body) |
119 }) | 119 }) |
120 Convey("malformed body", func() { | 120 Convey("malformed body", func() { |
121 err := read(mtPRPCText, []byte{0}) | 121 err := read(mtPRPCText, []byte{0}) |
122 So(err, ShouldNotBeNil) | 122 So(err, ShouldNotBeNil) |
123 » » » » So(ErrorStatus(err), ShouldEqual, http.StatusBad
Request) | 123 » » » » So(err.status, ShouldEqual, http.StatusBadReques
t) |
124 }) | 124 }) |
125 Convey("empty body", func() { | 125 Convey("empty body", func() { |
126 err := read(mtPRPCText, nil) | 126 err := read(mtPRPCText, nil) |
127 So(err, ShouldBeNil) | 127 So(err, ShouldBeNil) |
128 }) | 128 }) |
129 }) | 129 }) |
130 | 130 |
131 Convey("unsupported media type", func() { | 131 Convey("unsupported media type", func() { |
132 err := read("blah", nil) | 132 err := read("blah", nil) |
133 So(err, ShouldNotBeNil) | 133 So(err, ShouldNotBeNil) |
134 » » » So(ErrorStatus(err), ShouldEqual, http.StatusUnsupported
MediaType) | 134 » » » So(err.status, ShouldEqual, http.StatusUnsupportedMediaT
ype) |
135 }) | 135 }) |
136 }) | 136 }) |
137 | 137 |
138 Convey("parseHeader", t, func() { | 138 Convey("parseHeader", t, func() { |
139 c := context.Background() | 139 c := context.Background() |
140 | 140 |
141 header := func(name, value string) http.Header { | 141 header := func(name, value string) http.Header { |
142 return http.Header{ | 142 return http.Header{ |
143 name: []string{value}, | 143 name: []string{value}, |
144 } | 144 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 }) | 218 }) |
219 Convey("Fails", func() { | 219 Convey("Fails", func() { |
220 c2, err := parseHeader(c, header("Name-B
in", "zzz")) | 220 c2, err := parseHeader(c, header("Name-B
in", "zzz")) |
221 So(c2, ShouldEqual, c) | 221 So(c2, ShouldEqual, c) |
222 So(err, ShouldErrLike, "Name-Bin header:
illegal base64 data at input byte 0") | 222 So(err, ShouldErrLike, "Name-Bin header:
illegal base64 data at input byte 0") |
223 }) | 223 }) |
224 }) | 224 }) |
225 }) | 225 }) |
226 }) | 226 }) |
227 } | 227 } |
OLD | NEW |