|
| 1 | +// Copyright 2016, Google, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +var express = require('express'); |
| 17 | +var path = require('path'); |
| 18 | +var proxyquire = require('proxyquire').noPreserveCache(); |
| 19 | +var request = require('supertest'); |
| 20 | + |
| 21 | +var SAMPLE_PATH = path.join(__dirname, '../app.js'); |
| 22 | + |
| 23 | +function getSample () { |
| 24 | + var testApp = express(); |
| 25 | + sinon.stub(testApp, 'listen').callsArg(1); |
| 26 | + var expressMock = sinon.stub().returns(testApp); |
| 27 | + |
| 28 | + var app = proxyquire(SAMPLE_PATH, { |
| 29 | + express: expressMock |
| 30 | + }); |
| 31 | + return { |
| 32 | + app: app, |
| 33 | + mocks: { |
| 34 | + express: expressMock |
| 35 | + } |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +describe('appengine/endpoints/app.js', function () { |
| 40 | + var sample; |
| 41 | + |
| 42 | + beforeEach(function () { |
| 43 | + sample = getSample(); |
| 44 | + |
| 45 | + assert(sample.mocks.express.calledOnce); |
| 46 | + assert(sample.app.listen.calledOnce); |
| 47 | + assert.equal(sample.app.listen.firstCall.args[0], process.env.PORT || 8080); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should echo a message', function (done) { |
| 51 | + request(sample.app) |
| 52 | + .post('/echo') |
| 53 | + .send({ message: 'foo' }) |
| 54 | + .expect(200) |
| 55 | + .expect(function (response) { |
| 56 | + assert.equal(response.body.message, 'foo'); |
| 57 | + }) |
| 58 | + .end(done); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should try to parse encoded info', function (done) { |
| 62 | + request(sample.app) |
| 63 | + .get('/auth/info/googlejwt') |
| 64 | + .expect(200) |
| 65 | + .expect(function (response) { |
| 66 | + assert.deepEqual(response.body, { id: 'anonymous' }); |
| 67 | + }) |
| 68 | + .end(done); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should successfully parse encoded info', function (done) { |
| 72 | + request(sample.app) |
| 73 | + .get('/auth/info/googlejwt') |
| 74 | + .set('X-Endpoint-API-UserInfo', new Buffer(JSON.stringify({ id: 'foo' })).toString('base64')) |
| 75 | + .expect(200) |
| 76 | + .expect(function (response) { |
| 77 | + assert.deepEqual(response.body, { id: 'foo' }); |
| 78 | + }) |
| 79 | + .end(done); |
| 80 | + }); |
| 81 | +}); |
0 commit comments