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(Handler .succeed(" success" ))
35+
36+ for {
37+ response <- routes.toRoutes.runZIO(
38+ Request .get(url " /test " ).addHeader(Header .Accept (MediaType .application.`json`)),
39+ )
40+ status = response.status
41+ } yield assertTrue(
42+ status.code == 401 ,
43+ status == Status .Unauthorized ,
44+ )
45+ },
46+ test(" missing non-auth header should still return 400" ) {
47+ val endpoint = Endpoint (Method .GET / " test" )
48+ .header(HeaderCodec .headerAs[String ](" X-Custom-Header" ))
49+ .out[String ]
50+
51+ val routes = endpoint.implementHandler(Handler .succeed(" success" ))
52+
53+ for {
54+ response <- routes.toRoutes.runZIO(
55+ Request .get(url " /test " ).addHeader(Header .Accept (MediaType .application.`json`)),
56+ )
57+ status = response.status
58+ } yield assertTrue(
59+ status.code == 400 ,
60+ status == Status .BadRequest ,
61+ )
62+ },
63+ )
64+
65+ }
0 commit comments