Skip to content

Commit 1f1c0c3

Browse files
committed
Add more tests
1 parent 71910a3 commit 1f1c0c3

3 files changed

Lines changed: 90 additions & 0 deletions

File tree

airframe-http-finagle/src/test/scala/wvlet/airframe/http/finagle/RPCErrorHandlingTest.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ class RPCErrorHandlingTest extends AirSpec {
4545
test("rpc error test") { (client: FinagleSyncClient) =>
4646
warn("Running RPC exception test. Some warning messages will be shown")
4747

48+
test("with stack trace in msgpack") {
49+
val req = Request(Method.Post, "/wvlet.airframe.http.finagle.RPCErrorHandlingTest.DemoApi/permissionCheck")
50+
51+
val resp = client.sendSafe(req)
52+
53+
val errorMsgPack = resp.contentBytes
54+
val ex = RPCException.fromMsgPack(errorMsgPack)
55+
56+
resp.statusCode shouldBe RPCStatus.PERMISSION_DENIED_U14.httpStatus.code
57+
ex.status shouldBe RPCStatus.PERMISSION_DENIED_U14
58+
ex.message shouldBe "permission denied"
59+
ex.metadata shouldBe Map("retry_count" -> 3)
60+
61+
val s = new StringWriter
62+
val p = new PrintWriter(s)
63+
ex.printStackTrace(p)
64+
p.flush()
65+
val stackTrace = s.toString
66+
stackTrace.contains("DemoApi.permissionCheck") shouldBe true
67+
}
68+
4869
test("with stack trace") {
4970
val req = Request(Method.Post, "/wvlet.airframe.http.finagle.RPCErrorHandlingTest.DemoApi/permissionCheck")
5071
req.accept = HttpHeader.MediaType.ApplicationJson

airframe-http/src/main/scala/wvlet/airframe/http/HttpMessage.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ trait HttpMessage[Raw] extends HttpMessageBase[Raw] {
9191
// HTTP header setting utility methods
9292
def withAccept(acceptType: String): Raw = withHeader(HttpHeader.Accept, acceptType)
9393
def withAcceptMsgPack: Raw = withHeader(HttpHeader.Accept, HttpHeader.MediaType.ApplicationMsgPack)
94+
def withAcceptJson: Raw = withHeader(HttpHeader.Accept, HttpHeader.MediaType.ApplicationJson)
9495
def withAllow(allow: String): Raw = withHeader(HttpHeader.Allow, allow)
9596
def withAuthorization(authorization: String): Raw = withHeader(HttpHeader.Authorization, authorization)
9697
def withCacheControl(cacheControl: String): Raw = withHeader(HttpHeader.CacheControl, cacheControl)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package wvlet.airframe.http
15+
16+
import wvlet.airspec.AirSpec
17+
18+
class HttpRequestTest extends AirSpec {
19+
20+
test("accept application/msgpack") {
21+
val r = Http.POST("/").withAcceptMsgPack
22+
r.acceptsMsgPack shouldBe true
23+
r.acceptsJson shouldBe false
24+
}
25+
26+
test("accept application/x-msgpack") {
27+
val r = Http.POST("/").withAccept("application/x-msgpack")
28+
r.acceptsMsgPack shouldBe true
29+
r.acceptsJson shouldBe false
30+
}
31+
32+
test("accept application/json;encoding=utf-8") {
33+
val r = Http.POST("/").withAcceptJson
34+
r.acceptsJson shouldBe true
35+
r.acceptsMsgPack shouldBe false
36+
}
37+
38+
test("accept application/json") {
39+
val r = Http.POST("/").withAccept("application/json")
40+
r.acceptsJson shouldBe true
41+
r.acceptsMsgPack shouldBe false
42+
}
43+
44+
test("content-type application/msgpack") {
45+
val r = Http.POST("/").withContentTypeMsgPack
46+
r.isContentTypeMsgPack shouldBe true
47+
r.isContentTypeJson shouldBe false
48+
}
49+
50+
test("content-type application/x-msgpack") {
51+
val r = Http.POST("/").withContentType("application/x-msgpack")
52+
r.isContentTypeMsgPack shouldBe true
53+
r.isContentTypeJson shouldBe false
54+
}
55+
56+
test("content-type application/json;encoding=utf-8") {
57+
val r = Http.POST("/").withContentTypeJson
58+
r.isContentTypeJson shouldBe true
59+
r.isContentTypeMsgPack shouldBe false
60+
}
61+
62+
test("content-type application/json") {
63+
val r = Http.POST("/").withContentType("application/json")
64+
r.isContentTypeJson shouldBe true
65+
r.isContentTypeMsgPack shouldBe false
66+
}
67+
68+
}

0 commit comments

Comments
 (0)