@@ -4,16 +4,25 @@ const express = require('express')
44const request = require ( 'supertest' )
55const { expect } = require ( 'chai' )
66
7+ const USER = 'https://ruben.verborgh.org/profile/#me'
8+
79describe ( 'Auth Proxy' , ( ) => {
810 describe ( 'An auth proxy with 2 destinations' , ( ) => {
911 let app
12+ let loggedIn = true
1013 before ( ( ) => {
1114 nock ( 'http://server-a.org' ) . persist ( )
1215 . get ( / ./ ) . reply ( 200 , addRequestDetails ( 'a' ) )
1316 nock ( 'https://server-b.org' ) . persist ( )
1417 . get ( / ./ ) . reply ( 200 , addRequestDetails ( 'b' ) )
1518
1619 app = express ( )
20+ app . use ( ( req , res , next ) => {
21+ if ( loggedIn ) {
22+ req . session = { userId : USER }
23+ }
24+ next ( )
25+ } )
1726 authProxy ( app , {
1827 '/server/a' : 'http://server-a.org' ,
1928 '/server/b' : 'https://server-b.org/foo/bar'
@@ -33,6 +42,11 @@ describe('Auth Proxy', () => {
3342 expect ( path ) . to . equal ( '/' )
3443 } )
3544
45+ it ( 'sets the User header on the proxy request' , ( ) => {
46+ const { headers } = response . body
47+ expect ( headers ) . to . have . property ( 'user' , USER )
48+ } )
49+
3650 it ( 'returns status code 200' , ( ) => {
3751 expect ( response ) . to . have . property ( 'statusCode' , 200 )
3852 } )
@@ -51,6 +65,11 @@ describe('Auth Proxy', () => {
5165 expect ( path ) . to . equal ( '/my/path?query=string' )
5266 } )
5367
68+ it ( 'sets the User header on the proxy request' , ( ) => {
69+ const { headers } = response . body
70+ expect ( headers ) . to . have . property ( 'user' , USER )
71+ } )
72+
5473 it ( 'returns status code 200' , ( ) => {
5574 expect ( response ) . to . have . property ( 'statusCode' , 200 )
5675 } )
@@ -69,6 +88,11 @@ describe('Auth Proxy', () => {
6988 expect ( path ) . to . equal ( '/foo/bar' )
7089 } )
7190
91+ it ( 'sets the User header on the proxy request' , ( ) => {
92+ const { headers } = response . body
93+ expect ( headers ) . to . have . property ( 'user' , USER )
94+ } )
95+
7296 it ( 'returns status code 200' , ( ) => {
7397 expect ( response ) . to . have . property ( 'statusCode' , 200 )
7498 } )
@@ -87,6 +111,38 @@ describe('Auth Proxy', () => {
87111 expect ( path ) . to . equal ( '/foo/bar/my/path?query=string' )
88112 } )
89113
114+ it ( 'sets the User header on the proxy request' , ( ) => {
115+ const { headers } = response . body
116+ expect ( headers ) . to . have . property ( 'user' , USER )
117+ } )
118+
119+ it ( 'returns status code 200' , ( ) => {
120+ expect ( response ) . to . have . property ( 'statusCode' , 200 )
121+ } )
122+ } )
123+
124+ describe ( 'responding to /server/a without a logged-in user' , ( ) => {
125+ let response
126+ before ( ( ) => {
127+ loggedIn = false
128+ return request ( app ) . get ( '/server/a' )
129+ . then ( res => { response = res } )
130+ } )
131+ after ( ( ) => {
132+ loggedIn = true
133+ } )
134+
135+ it ( 'proxies to http://server-a.org/' , ( ) => {
136+ const { server, path } = response . body
137+ expect ( server ) . to . equal ( 'a' )
138+ expect ( path ) . to . equal ( '/' )
139+ } )
140+
141+ it ( 'does not set the User header on the proxy request' , ( ) => {
142+ const { headers } = response . body
143+ expect ( headers ) . to . not . have . property ( 'user' )
144+ } )
145+
90146 it ( 'returns status code 200' , ( ) => {
91147 expect ( response ) . to . have . property ( 'statusCode' , 200 )
92148 } )
0 commit comments