1
1
import { describe , expect , it } from "../suite.ts" ;
2
- import { concat , httpResponsesMiddleware , respondNotFound } from "../../mod.ts" ;
2
+ import {
3
+ concat ,
4
+ httpResponsesMiddleware ,
5
+ respondNotFound ,
6
+ respondRedirect ,
7
+ } from "../../mod.ts" ;
3
8
4
9
describe ( "http responses middleware" , ( ) => {
5
10
it ( "can short circuit a middleware stack with a 404" , function * ( ) {
@@ -18,4 +23,46 @@ describe("http responses middleware", () => {
18
23
19
24
expect ( response . status ) . toEqual ( 404 ) ;
20
25
} ) ;
26
+
27
+ it ( "can short circuit a middleware stack with a temporary redirect" , function * ( ) {
28
+ let handler = concat (
29
+ httpResponsesMiddleware ( ) ,
30
+ function * ( ) {
31
+ return yield * respondRedirect ( "https://localhost/other.html" ) ;
32
+ } ,
33
+ ) ;
34
+
35
+ let request = new Request ( "http://localhost/test.html" ) ;
36
+
37
+ let response = yield * handler ( request , function * ( ) {
38
+ throw new Error ( `should not reach here.` ) ;
39
+ } ) ;
40
+
41
+ expect ( response . status ) . toEqual ( 307 ) ;
42
+ expect ( response . headers . get ( "location" ) ) . toEqual (
43
+ "https://localhost/other.html" ,
44
+ ) ;
45
+ } ) ;
46
+
47
+ it ( "can short circuit a middleware stack with a permanent redirect" , function * ( ) {
48
+ let handler = concat (
49
+ httpResponsesMiddleware ( ) ,
50
+ function * ( ) {
51
+ return yield * respondRedirect ( "https://localhost/other.html" , {
52
+ permanent : true ,
53
+ } ) ;
54
+ } ,
55
+ ) ;
56
+
57
+ let request = new Request ( "http://localhost/test.html" ) ;
58
+
59
+ let response = yield * handler ( request , function * ( ) {
60
+ throw new Error ( `should not reach here.` ) ;
61
+ } ) ;
62
+
63
+ expect ( response . status ) . toEqual ( 308 ) ;
64
+ expect ( response . headers . get ( "location" ) ) . toEqual (
65
+ "https://localhost/other.html" ,
66
+ ) ;
67
+ } ) ;
21
68
} ) ;
0 commit comments