1 | var supertest = require('supertest');
|
2 | var assert = require('assert');
|
3 | var shortid = require('shortid');
|
4 | var request = require('request');
|
5 | var through = require('through2');
|
6 | var log = require('../lib/log');
|
7 | var _ = require('lodash');
|
8 | var sinon = require('sinon');
|
9 | var path = require('path');
|
10 | var helper = require('../lib/helper');
|
11 | var sandboxServer = require('../lib/sandbox-server');
|
12 |
|
13 | require('dash-assert');
|
14 |
|
15 | describe('sandboxServer', function() {
|
16 | var self;
|
17 |
|
18 | before(function() {
|
19 | sinon.stub(log, 'write', _.noop());
|
20 | });
|
21 |
|
22 | beforeEach(function() {
|
23 | self = this;
|
24 |
|
25 | this.program = {
|
26 | profile: {
|
27 | name: 'default',
|
28 | endpoint: 'https://apphost.com',
|
29 | jwt: {
|
30 | token: '23523454'
|
31 | }
|
32 | },
|
33 | cwd: path.join(__dirname, './fixtures/sample-app'),
|
34 | baseDir: path.join(__dirname, './fixtures/sample-app/app'),
|
35 | virtualApp: {
|
36 | appId: shortid.generate(),
|
37 | name: 'appname'
|
38 | },
|
39 | virtualHost: 'apphost.com'
|
40 | };
|
41 |
|
42 | sinon.stub(request, 'post', function(options, callback) {
|
43 |
|
44 | if (options.url.indexOf('/dev/' + self.program.virtualApp.appId + '/upload') !== -1) {
|
45 |
|
46 |
|
47 | return through(function(chunk, enc, cb) {
|
48 | cb();
|
49 | }, function() {
|
50 | callback(null, {statusCode: 200});
|
51 | });
|
52 | } else if (options.url.indexOf('/dev/' + self.program.virtualApp.appId + '/notfound') !== -1) {
|
53 | callback(null, {statusCode: 200});
|
54 | }
|
55 | });
|
56 | });
|
57 |
|
58 | afterEach(function() {
|
59 | request.post.restore();
|
60 | });
|
61 |
|
62 | after(function() {
|
63 | log.write.restore();
|
64 | });
|
65 |
|
66 | it('serves static files', function(done) {
|
67 | var server = sandboxServer(this.program);
|
68 | supertest(server)
|
69 | .get('/js/app.js')
|
70 | .expect(200)
|
71 | .expect('Content-Type', /application\/javascript/)
|
72 | .expect(function(res) {
|
73 | assert.equal(res.text, '// app.js\n');
|
74 | })
|
75 | .end(done);
|
76 | });
|
77 |
|
78 | it('serves node_modules from root of app', function(done) {
|
79 | var server = sandboxServer(this.program);
|
80 | supertest(server)
|
81 | .get('/node_modules/some-module/module.js')
|
82 | .expect(200)
|
83 | .expect('Content-Type', /application\/javascript/)
|
84 | .expect(function(res) {
|
85 | assert.equal(res.text, '// module.js\n');
|
86 | })
|
87 | .end(done);
|
88 | });
|
89 |
|
90 | it('serves bower_components from root of app', function(done) {
|
91 | var server = sandboxServer(this.program);
|
92 | supertest(server)
|
93 | .get('/bower_components/component/component.js')
|
94 | .expect(200)
|
95 | .expect('Content-Type', /application\/javascript/)
|
96 | .expect(function(res) {
|
97 | assert.equal(res.text, '// component.js\n');
|
98 | })
|
99 | .end(done);
|
100 | });
|
101 |
|
102 | it('missing file returns 404', function(done) {
|
103 | var server = sandboxServer(this.program);
|
104 | supertest(server)
|
105 | .get('/js/missing.js')
|
106 | .expect(404)
|
107 | .expect(function() {
|
108 | assert.isFalse(request.post.called);
|
109 | })
|
110 | .end(done);
|
111 | });
|
112 |
|
113 | describe('/sandbox route', function() {
|
114 | beforeEach(function(done) {
|
115 | helper.fileHash(path.join(this.program.baseDir, 'index.html'), function(err, hash) {
|
116 | if (err) return done(err);
|
117 |
|
118 | self.hash = hash;
|
119 | done();
|
120 | });
|
121 | });
|
122 |
|
123 | it('hash is different', function(done) {
|
124 | var server = sandboxServer(this.program);
|
125 | var redirectUrl = 'https://appname--dev.apphost.com/';
|
126 |
|
127 | supertest(server)
|
128 | .get('/sandbox/index.html?hash=not_a_real_hash&return=' + encodeURIComponent(redirectUrl))
|
129 | .expect(302)
|
130 | .expect(function(res) {
|
131 | var apiUploadUrl = self.program.profile.endpoint + '/api/dev/' + self.program.virtualApp.appId + '/upload/index.html';
|
132 | assert.ok(request.post.calledWith(sinon.match({url: apiUploadUrl})));
|
133 |
|
134 | assert.equal(res.headers.location, redirectUrl);
|
135 | })
|
136 | .end(done);
|
137 | });
|
138 |
|
139 | it('hash value is the same', function(done) {
|
140 | var server = sandboxServer(this.program);
|
141 | var redirectUrl = 'https://appname--dev.apphost.com/';
|
142 |
|
143 | supertest(server)
|
144 | .get('/sandbox/index.html?hash=' + this.hash + '&return=' + encodeURIComponent(redirectUrl))
|
145 | .expect(302)
|
146 | .expect(function(res) {
|
147 | assert.isFalse(request.post.called);
|
148 | assert.equal(res.headers.location, redirectUrl);
|
149 | })
|
150 | .end(done);
|
151 | });
|
152 |
|
153 | it('missing file without custom 404', function(done) {
|
154 | var server = sandboxServer(this.program);
|
155 | var redirectUrl = 'https://appname--dev.apphost.com/missing';
|
156 |
|
157 | supertest(server)
|
158 | .get('/sandbox/missing.html?return=' + encodeURIComponent(redirectUrl))
|
159 | .expect(302)
|
160 | .expect(function() {
|
161 | assert.isTrue(request.post.calledWith(sinon.match({
|
162 | url: 'https://apphost.com/api/dev/' + self.program.virtualApp.appId + '/notfound/missing.html'
|
163 | })));
|
164 | })
|
165 | .end(done);
|
166 | });
|
167 |
|
168 | it('missing file with custom404 query parameter', function(done) {
|
169 | var server = sandboxServer(this.program);
|
170 | var redirectUrl = 'https://appname--dev.apphost.com/missing';
|
171 |
|
172 | supertest(server)
|
173 | .get('/sandbox/missing.html?custom404=404.html&return=' + encodeURIComponent(redirectUrl))
|
174 | .expect(302)
|
175 | .expect(function() {
|
176 | assert.isTrue(request.post.calledWith(sinon.match({
|
177 | url: 'https://apphost.com/api/dev/' + self.program.virtualApp.appId + '/upload/404.html'
|
178 | })));
|
179 | })
|
180 | .end(done);
|
181 | });
|
182 |
|
183 | it('missing custom 404 page', function(done) {
|
184 | var server = sandboxServer(this.program);
|
185 | var redirectUrl = 'https://appname--dev.apphost.com/missing';
|
186 |
|
187 | supertest(server)
|
188 | .get('/sandbox/missing.html?custom404=missing404.html&return=' + encodeURIComponent(redirectUrl))
|
189 | .expect(302)
|
190 | .expect(function() {
|
191 | assert.isTrue(request.post.calledWith(sinon.match({
|
192 | url: 'https://apphost.com/api/dev/' + self.program.virtualApp.appId + '/notfound/missing.html'
|
193 | })));
|
194 | })
|
195 | .end(done);
|
196 | });
|
197 |
|
198 | it('missing favicon', function(done) {
|
199 | var server = sandboxServer(this.program);
|
200 |
|
201 | supertest(server)
|
202 | .get('/favicon.ico')
|
203 | .expect(302)
|
204 | .expect(function(res) {
|
205 | assert.equal(res.headers.location, 'http://appname--dev.apphost.com/favicon.ico?default=1');
|
206 | })
|
207 | .end(done);
|
208 | });
|
209 | });
|
210 | });
|