|
| 1 | +/* |
| 2 | + * Copyright 2021 - 2023 Sporta Technologies PVT LTD & the ZIO HTTP contributors. |
| 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 zio.http.endpoint |
| 18 | + |
| 19 | +import zio._ |
| 20 | +import zio.test._ |
| 21 | + |
| 22 | +import zio.http._ |
| 23 | +import zio.http.codec._ |
| 24 | +import zio.http.endpoint._ |
| 25 | + |
| 26 | +object MissingAuthHeaderSpec extends ZIOHttpSpec { |
| 27 | + |
| 28 | + def spec = suite("MissingAuthHeaderSpec")( |
| 29 | + test("missing Authorization header should return 401 not 400") { |
| 30 | + val endpoint = Endpoint(Method.GET / "test") |
| 31 | + .header(HeaderCodec.authorization) |
| 32 | + .out[String] |
| 33 | + |
| 34 | + val routes = endpoint.implementHandler( |
| 35 | + Handler.succeed("success"), |
| 36 | + ) |
| 37 | + |
| 38 | + for { |
| 39 | + response <- routes.toRoutes.runZIO( |
| 40 | + Request.get(url"/test").addHeader(Header.Accept(MediaType.application.`json`)), |
| 41 | + ) |
| 42 | + status = response.status |
| 43 | + } yield assertTrue( |
| 44 | + status.code == 401, |
| 45 | + status == Status.Unauthorized, |
| 46 | + ) |
| 47 | + }, |
| 48 | + test("missing non-auth header should still return 400") { |
| 49 | + val endpoint = Endpoint(Method.GET / "test") |
| 50 | + .header(HeaderCodec.headerAs[String]("X-Custom-Header")) |
| 51 | + .out[String] |
| 52 | + |
| 53 | + val routes = endpoint.implementHandler( |
| 54 | + Handler.succeed("success"), |
| 55 | + ) |
| 56 | + |
| 57 | + for { |
| 58 | + response <- routes.toRoutes.runZIO( |
| 59 | + Request.get(url"/test").addHeader(Header.Accept(MediaType.application.`json`)), |
| 60 | + ) |
| 61 | + status = response.status |
| 62 | + } yield assertTrue( |
| 63 | + status.code == 400, |
| 64 | + status == Status.BadRequest, |
| 65 | + ) |
| 66 | + }, |
| 67 | + ) |
| 68 | + |
| 69 | +} |
0 commit comments