11import { assert , assertEquals } from "https://deno.land/std@0.70.0/testing/asserts.ts" ;
2+ import { compose , converge , mergeRight } from "https://x.nest.land/ramda@0.27.0/source/index.js" ;
23
4+ import Either from "https://deno.land/x/functional@v1.1.0/library/Either.js" ;
35import Task from "https://deno.land/x/functional@v1.1.0/library/Task.js" ;
46import { decodeRaw , encodeText , safeExtract } from "https://deno.land/x/functional@v1.1.0/library/utilities.js" ;
57import { fetch } from "https://deno.land/x/functional_io@v0.5.0/library/browser_safe.js" ;
@@ -8,17 +10,21 @@ import Response from "https://deno.land/x/functional_io@v0.5.0/library/Response.
810
911import { handlers , route } from "./route.js" ;
1012import { startHTTPServer } from "./server.js" ;
11- import { authorizeRequest , explodeRequest } from "./utilities.js" ;
13+ import { factorizeMiddleware , explodeRequest } from "./utilities.js" ;
1214
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+ ) ;
1420
1521const routeHandlers = [
1622 handlers . get ( '/' , _ => Task . of ( Response . OK ( { } , new Uint8Array ( [ ] ) ) ) ) ,
1723 handlers . post ( '/hoge' , request => Task . of ( Response . OK ( { } , request . raw ) ) ) ,
1824 handlers . get (
1925 '/hoge' ,
2026 explodeRequest (
21- ( { 'filters[status]' : status } ) =>
27+ ( { status } ) =>
2228 Task . of ( Response . OK ( { } , encodeText ( JSON . stringify ( { status } ) ) ) )
2329 )
2430 ) ,
@@ -28,17 +34,13 @@ const routeHandlers = [
2834 ) ,
2935 handlers . put (
3036 '/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 ) ) ) ) )
3538 ) ,
3639 handlers . post (
3740 '/fuga/piyo' ,
3841 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 } ) ) ) )
4244 )
4345 )
4446] ;
@@ -79,11 +81,11 @@ Deno.test(
7981) ;
8082
8183Deno . test (
82- "startHTTPServer with explodeRequest: GET /hoge" ,
84+ "startHTTPServer with explodeRequest: GET /hoge?status=active " ,
8385 async ( ) => {
8486 const server = startHTTPServer ( { port : 8080 } , route ( ...routeHandlers ) ) ;
8587
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 ( )
8789
8890 const response = safeExtract ( "Failed to unpack the response" , container ) ;
8991
@@ -169,3 +171,31 @@ Deno.test(
169171 server . close ( ) ;
170172 }
171173) ;
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