1
1
import { assert , assertEquals } from "https://deno.land/[email protected] /testing/asserts.ts" ;
2
+ import { compose , converge , mergeRight } from "https://x.nest.land/[email protected] /source/index.js" ;
2
3
4
+ import Either from "https://deno.land/x/[email protected] /library/Either.js" ;
3
5
import Task from "https://deno.land/x/[email protected] /library/Task.js" ;
4
6
import { decodeRaw , encodeText , safeExtract } from "https://deno.land/x/[email protected] /library/utilities.js" ;
5
7
import { fetch } from "https://deno.land/x/[email protected] /library/browser_safe.js" ;
@@ -8,17 +10,21 @@ import Response from "https://deno.land/x/
[email protected] /library/Response.
8
10
9
11
import { handlers , route } from "./route.js" ;
10
12
import { startHTTPServer } from "./server.js" ;
11
- import { authorizeRequest , explodeRequest } from "./utilities.js" ;
13
+ import { factorizeMiddleware , explodeRequest } from "./utilities.js" ;
12
14
13
- const authorize = authorizeRequest ( _ => Task . of ( { authorizationToken : "hoge" } ) ) ;
15
+ const authorize = factorizeMiddleware ( request =>
16
+ request . headers [ "accept" ] === 'application/json'
17
+ ? Task . of ( { authorizationToken : "hoge" } )
18
+ : Task ( _ => Either . Left ( Response . BadRequest ( { } , new Uint8Array ( [ ] ) ) ) )
19
+ ) ;
14
20
15
21
const routeHandlers = [
16
22
handlers . get ( '/' , _ => Task . of ( Response . OK ( { } , new Uint8Array ( [ ] ) ) ) ) ,
17
23
handlers . post ( '/hoge' , request => Task . of ( Response . OK ( { } , request . raw ) ) ) ,
18
24
handlers . get (
19
25
'/hoge' ,
20
26
explodeRequest (
21
- ( { 'filters[status]' : status } ) =>
27
+ ( { status } ) =>
22
28
Task . of ( Response . OK ( { } , encodeText ( JSON . stringify ( { status } ) ) ) )
23
29
)
24
30
) ,
@@ -28,17 +34,13 @@ const routeHandlers = [
28
34
) ,
29
35
handlers . put (
30
36
'/hoge' ,
31
- explodeRequest (
32
- ( meta , body ) =>
33
- Task . of ( Response . OK ( { } , encodeText ( JSON . stringify ( body ) ) ) )
34
- )
37
+ explodeRequest ( ( meta , body ) => Task . of ( Response . OK ( { } , encodeText ( JSON . stringify ( body ) ) ) ) )
35
38
) ,
36
39
handlers . post (
37
40
'/fuga/piyo' ,
38
41
authorize (
39
- // explodeRequest(
40
- ( { authorizationToken } ) => Task . of ( Response . OK ( { } , encodeText ( JSON . stringify ( { authorizationToken } ) ) ) )
41
- // )
42
+ ( { authorizationToken } ) =>
43
+ Task . of ( Response . OK ( { } , encodeText ( JSON . stringify ( { authorizationToken } ) ) ) )
42
44
)
43
45
)
44
46
] ;
@@ -79,11 +81,11 @@ Deno.test(
79
81
) ;
80
82
81
83
Deno . test (
82
- "startHTTPServer with explodeRequest: GET /hoge" ,
84
+ "startHTTPServer with explodeRequest: GET /hoge?status=active " ,
83
85
async ( ) => {
84
86
const server = startHTTPServer ( { port : 8080 } , route ( ...routeHandlers ) ) ;
85
87
86
- const container = await fetch ( Request . GET ( 'http://localhost:8080/hoge?filters[ status] =active' ) ) . run ( )
88
+ const container = await fetch ( Request . GET ( 'http://localhost:8080/hoge?status=active' ) ) . run ( )
87
89
88
90
const response = safeExtract ( "Failed to unpack the response" , container ) ;
89
91
@@ -169,3 +171,31 @@ Deno.test(
169
171
server . close ( ) ;
170
172
}
171
173
) ;
174
+
175
+ Deno . test (
176
+ "startHTTPServer with explodeRequest: POST /fuga/piyo -- unauthorized" ,
177
+ async ( ) => {
178
+ const server = startHTTPServer ( { port : 8080 } , route ( ...routeHandlers ) ) ;
179
+
180
+ const container = await fetch (
181
+ Request (
182
+ {
183
+ headers : {
184
+ 'accept' : 'text/plain' ,
185
+ 'content-type' : 'application/json'
186
+ } ,
187
+ method : 'POST' ,
188
+ url : 'http://localhost:8080/fuga/piyo'
189
+ } ,
190
+ new Uint8Array ( [ ] )
191
+ )
192
+ ) . run ( )
193
+
194
+ const response = safeExtract ( "Failed to unpack the response" , container ) ;
195
+
196
+ assert ( Response . is ( response ) ) ;
197
+ assertEquals ( response . headers . status , 400 ) ;
198
+
199
+ server . close ( ) ;
200
+ }
201
+ ) ;
0 commit comments