|
| 1 | +/* |
| 2 | + * Copyright 2021 Typelevel |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package feral.lambda |
| 18 | +package http4s |
| 19 | + |
| 20 | +import cats.effect.IO |
| 21 | +import cats.syntax.all._ |
| 22 | +import feral.lambda.events.ApiGatewayProxyEvent |
| 23 | +import feral.lambda.events.ApiGatewayProxyEventSuite.* |
| 24 | +import munit.CatsEffectSuite |
| 25 | +import org.http4s.Headers |
| 26 | +import org.http4s.HttpApp |
| 27 | +import org.http4s.Method |
| 28 | +import org.http4s.syntax.all._ |
| 29 | + |
| 30 | +class ApiGatewayProxyHandlerSuite extends CatsEffectSuite { |
| 31 | + |
| 32 | + val expectedHeaders: Headers = Headers( |
| 33 | + "Accept-Language" -> "en-US,en;q=0.8", |
| 34 | + "CloudFront-Is-Mobile-Viewer" -> "false", |
| 35 | + "CloudFront-Is-Desktop-Viewer" -> "true", |
| 36 | + "Via" -> "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", |
| 37 | + "X-Amz-Cf-Id" -> "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", |
| 38 | + "Host" -> "1234567890.execute-api.us-east-1.amazonaws.com", |
| 39 | + "Accept-Encoding" -> "gzip, deflate, sdch", |
| 40 | + "X-Forwarded-Port" -> "443", |
| 41 | + "Cache-Control" -> "max-age=0", |
| 42 | + "CloudFront-Viewer-Country" -> "US", |
| 43 | + "CloudFront-Is-SmartTV-Viewer" -> "false", |
| 44 | + "X-Forwarded-Proto" -> "https", |
| 45 | + "Upgrade-Insecure-Requests" -> "1", |
| 46 | + "User-Agent" -> "Custom User Agent String", |
| 47 | + "CloudFront-Forwarded-Proto" -> "https", |
| 48 | + "X-Forwarded-For" -> "127.0.0.1, 127.0.0.2", |
| 49 | + "CloudFront-Is-Tablet-Viewer" -> "false", |
| 50 | + "Accept" -> "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" |
| 51 | + ) |
| 52 | + |
| 53 | + val expectedBody: String = """{"test":"body"}""" |
| 54 | + |
| 55 | + test("decode event") { |
| 56 | + for { |
| 57 | + event <- event.as[ApiGatewayProxyEvent].liftTo[IO] |
| 58 | + request <- ApiGatewayProxyHandler.decodeEvent[IO](event) |
| 59 | + _ <- IO(assertEquals(request.method, Method.POST)) |
| 60 | + _ <- IO(assertEquals(request.uri, uri"/path/to/resource?foo=bar&foo=bar")) |
| 61 | + _ <- IO(assertEquals(request.headers, expectedHeaders)) |
| 62 | + responseBody <- request.bodyText.compile.string |
| 63 | + _ <- IO(assertEquals(responseBody, expectedBody)) |
| 64 | + } yield () |
| 65 | + } |
| 66 | + |
| 67 | + // compile-only test |
| 68 | + def handler(implicit inv: ApiGatewayProxyInvocation[IO]) = |
| 69 | + ApiGatewayProxyHandler(HttpApp.notFound[IO]) |
| 70 | + |
| 71 | +} |
0 commit comments