Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Side by Side Diff: server/prpc/decoding_test.go

Issue 1636873006: server/prpc: updated server according to protocol (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Removed some panics, malformed JSON now errors. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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, ShouldNotBeNil)
112 » » » » So(err.status, ShouldEqual, http.StatusBadReques t)
112 }) 113 })
113 }) 114 })
114 115
115 Convey("text", func() { 116 Convey("text", func() {
116 Convey(mtPRPCText, func() { 117 Convey(mtPRPCText, func() {
117 body := []byte(`name: "Lucy"`) 118 body := []byte(`name: "Lucy"`)
118 testLucy(mtPRPCText, body) 119 testLucy(mtPRPCText, body)
119 }) 120 })
120 Convey("malformed body", func() { 121 Convey("malformed body", func() {
121 err := read(mtPRPCText, []byte{0}) 122 err := read(mtPRPCText, []byte{0})
122 So(err, ShouldNotBeNil) 123 So(err, ShouldNotBeNil)
123 » » » » So(ErrorStatus(err), ShouldEqual, http.StatusBad Request) 124 » » » » So(err.status, ShouldEqual, http.StatusBadReques t)
124 }) 125 })
125 Convey("empty body", func() { 126 Convey("empty body", func() {
126 err := read(mtPRPCText, nil) 127 err := read(mtPRPCText, nil)
127 So(err, ShouldBeNil) 128 So(err, ShouldBeNil)
128 }) 129 })
129 }) 130 })
130 131
131 Convey("unsupported media type", func() { 132 Convey("unsupported media type", func() {
132 err := read("blah", nil) 133 err := read("blah", nil)
133 So(err, ShouldNotBeNil) 134 So(err, ShouldNotBeNil)
134 » » » So(ErrorStatus(err), ShouldEqual, http.StatusUnsupported MediaType) 135 » » » So(err.status, ShouldEqual, http.StatusUnsupportedMediaT ype)
135 }) 136 })
136 }) 137 })
137 138
138 Convey("parseHeader", t, func() { 139 Convey("parseHeader", t, func() {
139 c := context.Background() 140 c := context.Background()
140 141
141 header := func(name, value string) http.Header { 142 header := func(name, value string) http.Header {
142 return http.Header{ 143 return http.Header{
143 name: []string{value}, 144 name: []string{value},
144 } 145 }
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 }) 219 })
219 Convey("Fails", func() { 220 Convey("Fails", func() {
220 c2, err := parseHeader(c, header("Name-B in", "zzz")) 221 c2, err := parseHeader(c, header("Name-B in", "zzz"))
221 So(c2, ShouldEqual, c) 222 So(c2, ShouldEqual, c)
222 So(err, ShouldErrLike, "Name-Bin header: illegal base64 data at input byte 0") 223 So(err, ShouldErrLike, "Name-Bin header: illegal base64 data at input byte 0")
223 }) 224 })
224 }) 225 })
225 }) 226 })
226 }) 227 })
227 } 228 }
OLDNEW
« no previous file with comments | « server/prpc/decoding.go ('k') | server/prpc/encoding.go » ('j') | server/prpc/encoding.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698