1 | var t = require('../test-lib/test.js');
|
2 | var assert = require('assert');
|
3 | var request = require('request');
|
4 |
|
5 | var apos;
|
6 |
|
7 | describe('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 |
|
53 | assert.equal(response.statusCode, 200);
|
54 |
|
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 |
|
68 | assert.equal(response.statusCode, 200);
|
69 |
|
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 |
|
82 | assert.equal(response.statusCode, 302);
|
83 |
|
84 | assert.equal(response.headers['location'], '/child-moved');
|
85 | return done();
|
86 | });
|
87 | });
|
88 |
|
89 | });
|
90 |
|
91 | describe('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 |
|
141 | assert.equal(response.statusCode, 200);
|
142 |
|
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 |
|
159 | assert.equal(response.statusCode, 301);
|
160 |
|
161 | assert.equal(response.headers['location'], '/child-moved');
|
162 | return done();
|
163 | });
|
164 | });
|
165 |
|
166 | });
|