1
1
'use strict' ;
2
2
3
- const event = require ( `${ PACKAGE_ROOT } /event.json` ) ;
4
- const chai = require ( 'chai' ) ;
3
+ const event = require ( `${ PACKAGE_ROOT } /event.json` ) ;
4
+ const chai = require ( 'chai' ) ;
5
+ const chaiAsPromised = require ( 'chai-as-promised' ) ;
6
+ const sinon = require ( 'sinon' ) ;
7
+ const https = require ( 'https' ) ;
8
+
9
+ chai . use ( chaiAsPromised ) ;
5
10
6
11
const expect = chai . expect ;
7
12
8
13
// Load modules.
9
- const Request = require ( '@lambda-lambda-lambda/router/src/router/Request.js' ) ;
10
- const Response = require ( '@lambda-lambda-lambda/router/src/router/Response.js' ) ;
11
- const Stack = require ( '@lambda-lambda-lambda/router/src/router/Stack.js' ) ;
12
- const Utils = require ( '@lambda-lambda-lambda/router/src/router/Utils.js' ) ;
13
- const middleware = require ( PLUGIN_ROOT ) ;
14
+ const { RouterError} = require ( '@lambda-lambda-lambda/router/src/router/Error' ) ;
15
+ const Request = require ( '@lambda-lambda-lambda/router/src/router/Request.js' ) ;
16
+ const Response = require ( '@lambda-lambda-lambda/router/src/router/Response.js' ) ;
17
+ const Stack = require ( '@lambda-lambda-lambda/router/src/router/Stack.js' ) ;
18
+ const Utils = require ( '@lambda-lambda-lambda/router/src/router/Utils.js' ) ;
19
+ const middleware = require ( PLUGIN_ROOT ) ;
20
+
21
+ afterEach ( ( ) => {
22
+ sinon . restore ( ) ;
23
+ } ) ;
14
24
15
25
describe ( 'GoogleReCaptchaHandler' , function ( ) {
16
26
describe ( 'success' , function ( ) {
@@ -33,7 +43,7 @@ describe('GoogleReCaptchaHandler', function() {
33
43
Utils . setFuncName ( dependency , 'middleware' ) ;
34
44
Utils . setFuncName ( middleware , 'middleware' ) ;
35
45
36
- const route = function ( req , res ) {
46
+ const route = async function ( req , res , next ) {
37
47
res . status ( 200 ) . send ( ) ;
38
48
} ;
39
49
@@ -69,4 +79,50 @@ describe('GoogleReCaptchaHandler', function() {
69
79
expect ( result . body ) . to . be . undefined ;
70
80
} ) ;
71
81
} ) ;
82
+
83
+ describe ( 'error' , function ( ) {
84
+ const stack = new Stack ( ) ;
85
+
86
+ const dependency = function ( req , res , next ) {
87
+ const config = {
88
+ google : {
89
+ reCaptcha : {
90
+ secretKey : ''
91
+ }
92
+ }
93
+ } ;
94
+
95
+ req . plugin ( 'config' , config ) ;
96
+ next ( ) ;
97
+ } ;
98
+
99
+ Utils . setFuncName ( dependency , 'middleware' ) ;
100
+ Utils . setFuncName ( middleware , 'middleware' ) ;
101
+
102
+ const route = async function ( req , res , next ) {
103
+ res . status ( 200 ) . send ( ) ;
104
+ } ;
105
+
106
+ Utils . setFuncName ( route , 'route:index' ) ;
107
+
108
+ stack . middleware = [ dependency , middleware ] ;
109
+ stack . routes = route ;
110
+
111
+ // Define form POST parameters.
112
+ event . Records [ 0 ] . cf . request . method = 'POST' ;
113
+ event . Records [ 0 ] . cf . request . uri = '/path/to/resource' ;
114
+ event . Records [ 0 ] . cf . request . body = {
115
+ data : Buffer . from ( 'g-recaptcha-response=GOOGLE_RESPONSE' )
116
+ . toString ( 'base64' )
117
+ } ;
118
+
119
+ const req = new Request ( event . Records [ 0 ] . cf . request , { } ) ;
120
+ const res = new Response ( { } ) ;
121
+
122
+ it ( 'should throw RouterError' , function ( ) {
123
+ const result = stack . exec ( req , res ) ;
124
+
125
+ return expect ( result ) . to . be . rejectedWith ( RouterError , / M i s s i n g G o o g l e A P I s e c r e t k e y / ) ;
126
+ } ) ;
127
+ } ) ;
72
128
} ) ;
0 commit comments