diffy
dbe1c9dd948f
added body extraction for post requests
Puneet Khanduri
21 days ago

40 41 42 43 44 45 46 47 48 49 50 40 41 42 43 44 45 46 47 48 49 50 51
} else Map.empty } def liftRequest(req: HttpRequest): Future[Message] = { val canonicalResource = Option(req.headers.get("Canonical-Resource"))
Future.value(Message(canonicalResource, FieldMap(Map("request"-> req.toString))))
val body = req.getContent.copy.toString(Charsets.Utf8) Future.value(Message(canonicalResource, FieldMap(Map("request"-> req.toString, "body" -> body))))
} def liftResponse(resp: Try[HttpResponse]): Future[Message] = { Future.const(resp) flatMap { r: HttpResponse => val mediaTypeOpt: Option[MediaType] =
...
63 64 65 66 67 68 69 70 71 72 73 64 65 66 67 68 69 70 71 72 73 74
) /** When Content-Type is set as application/json, lift as Json **/ case (Some(mediaType), _) if mediaType.is(MediaType.JSON_UTF_8) || mediaType.toString == "application/json" => { val jsonContentTry = Try {
JsonLifter.decode(r.getContent.toString(Charsets.Utf8))
JsonLifter.decode(r.getContent.copy.toString(Charsets.Utf8))
} Future.const(jsonContentTry map { jsonContent => val responseMap = Map( r.getStatus.getCode.toString -> (Map(
...
84 85 86 87 88 89 90 91 92 93 94 85 86 87 88 89 90 91 92 93 94 95
/** When Content-Type is set as text/html, lift as Html **/ case (Some(mediaType), _) if mediaType.is(MediaType.HTML_UTF_8) || mediaType.toString == "text/html" => { val htmlContentTry = Try {
HtmlLifter.lift(HtmlLifter.decode(r.getContent.toString(Charsets.Utf8)))
HtmlLifter.lift(HtmlLifter.decode(r.getContent.copy.toString(Charsets.Utf8)))
} Future.const(htmlContentTry map { htmlContent => val responseMap = Map( r.getStatus.getCode.toString -> (Map(

47 48 49 50 51 52 53 54 55 56 57 58 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
val validHtmlBody = """<html><head><title>Sample HTML</title></head><body><div class="header"><h1 class="box">Hello World</h1></div><p>Lorem ipsum dolor sit amet.</p></body></html>""" val testException = new Exception("test exception")
def request(method: HttpMethod, uri: String): HttpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, uri)
def request(method: HttpMethod, uri: String, body: Option[String] = None): HttpRequest = { val req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, uri) body foreach { b => req.setContent(ChannelBuffers.wrappedBuffer(b.getBytes(Charsets.Utf8)))} req }
def response(status: HttpResponseStatus, body: String): HttpResponse = { val resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status) resp.headers() .add(HttpHeaders.Names.CONTENT_LENGTH, body.length)
...
75 76 77 78 79 80 81 82 83 84 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
val resultFieldMap = msg.result.asInstanceOf[FieldMap[String]] msg.endpoint.get should equal ("endpoint") resultFieldMap.get("request").get should equal (req.toString) }
it("lift simple Post request") { val lifter = new HttpLifter(false) val requestBody = "request_body" val req = request(HttpMethod.POST, reqUri, Some(requestBody)) req.headers().add("Canonical-Resource", "endpoint") val msg = Await.result(lifter.liftRequest(req)) val resultFieldMap = msg.result.asInstanceOf[FieldMap[String]] msg.endpoint.get should equal ("endpoint") resultFieldMap.get("request").get should equal (req.toString) resultFieldMap.get("body").get should equal (requestBody) }
} describe("LiftResponse") { it("lift simple Json response") { checkJsonContentTypeIsLifted(MediaType.JSON_UTF_8.toString)
About FluentSend Feedback