UNPKG

4.54 kBJavaScriptView Raw
1var t = require('../test-lib/test.js');
2var assert = require('assert');
3var request = require('request');
4
5var apos;
6
7describe('Soft Redirects', function() {
8
9 this.timeout(t.timeout);
10
11 after(function(done) {
12 return t.destroy(apos, done);
13 });
14
15 it('should exist', function(done) {
16 apos = require('../index.js')({
17 root: module,
18 shortName: 'test',
19
20 modules: {
21 'apostrophe-express': {
22 port: 7900,
23 secret: 'test'
24 },
25 'apostrophe-pages': {
26 park: [
27 {
28 parkedId: 'child',
29 title: 'Child',
30 slug: '/child',
31 type: 'default',
32 published: true
33 }
34 ]
35 }
36 },
37 afterInit: function(callback) {
38 assert(apos.modules['apostrophe-soft-redirects']);
39 apos.argv._ = [];
40 return callback(null);
41 },
42 afterListen: function(err) {
43 assert(!err);
44 done();
45 }
46 });
47 });
48
49 it('should be able to serve the /child page (which also populates historicUrls)', function(done) {
50 return request('http://localhost:7900/child', function(err, response, body) {
51 assert(!err);
52 // Is our status code good?
53 assert.equal(response.statusCode, 200);
54 // Did we get our page back?
55 assert(body.match(/Default Page Template/));
56 return done();
57 });
58 });
59
60 it('should be able to change the URL via db', function() {
61 return apos.docs.db.update({ slug: '/child' }, { $set: { slug: '/child-moved' } });
62 });
63
64 it('should be able to serve the page at its new URL', function(done) {
65 return request('http://localhost:7900/child-moved', function(err, response, body) {
66 assert(!err);
67 // Is our status code good?
68 assert.equal(response.statusCode, 200);
69 // Did we get our page back?
70 assert(body.match(/Default Page Template/));
71 return done();
72 });
73 });
74
75 it('should be able to serve the page at its old URL too, via redirect', function(done) {
76 return request({
77 url: 'http://localhost:7900/child',
78 followRedirect: false
79 }, function(err, response, body) {
80 assert(!err);
81 // Is our status code good?
82 assert.equal(response.statusCode, 302);
83 // Are we going to be redirected to our page?
84 assert.equal(response.headers['location'], '/child-moved');
85 return done();
86 });
87 });
88
89});
90
91describe('Soft Redirects - with `statusCode` option', function() {
92
93 this.timeout(t.timeout);
94
95 after(function(done) {
96 return t.destroy(apos, done);
97 });
98
99 it('should exist', function(done) {
100 apos = require('../index.js')({
101 root: module,
102 shortName: 'test',
103
104 modules: {
105 'apostrophe-express': {
106 port: 7900,
107 secret: 'test'
108 },
109 'apostrophe-pages': {
110 park: [
111 {
112 parkedId: 'child',
113 title: 'Child',
114 slug: '/child',
115 type: 'default',
116 published: true
117 }
118 ]
119 },
120 'apostrophe-soft-redirects': {
121 statusCode: 301
122 }
123 },
124 afterInit: function(callback) {
125 assert(apos.modules['apostrophe-soft-redirects']);
126 assert.equal(apos.modules['apostrophe-soft-redirects'].options.statusCode, 301);
127 apos.argv._ = [];
128 return callback(null);
129 },
130 afterListen: function(err) {
131 assert(!err);
132 done();
133 }
134 });
135 });
136
137 it('should be able to serve the /child page (which also populates historicUrls)', function(done) {
138 return request('http://localhost:7900/child', function(err, response, body) {
139 assert(!err);
140 // Is our status code good?
141 assert.equal(response.statusCode, 200);
142 // Did we get our page back?
143 assert(body.match(/Default Page Template/));
144 return done();
145 });
146 });
147
148 it('should be able to change the URL via db', function() {
149 return apos.docs.db.update({ slug: '/child' }, { $set: { slug: '/child-moved' } });
150 });
151
152 it('should be able to serve the page at its old URL too, via redirect', function(done) {
153 return request({
154 url: 'http://localhost:7900/child',
155 followRedirect: false
156 }, function(err, response, body) {
157 assert(!err);
158 // Is our status code good?
159 assert.equal(response.statusCode, 301);
160 // Are we going to be redirected to our page?
161 assert.equal(response.headers['location'], '/child-moved');
162 return done();
163 });
164 });
165
166});