redirect.js
6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*eslint-env mocha*/
/*eslint no-invalid-this:0, max-nested-callbacks:[1, 6]*/
/*jshint -W100*/
var assert = require('power-assert');
var typeOf = require('type-of');
var helper = require('./_helper');
var cli = require('../index');
describe('redirect', function () {
before(function (done) {
cli.fetch(helper.url() + '?start_trace_route').finally(done);
});
after(function (done) {
cli.fetch(helper.url() + '?stop_trace_route').finally(done);
});
describe('async', function () {
describe('30x', function () {
it('documentInfoにリダイレクト先のURLが登録される', function (done) {
var url = helper.url('manual', 'euc-jp');
cli.fetch(helper.url('~redirect'), function (err, $, res, body) {
assert($.documentInfo().url === url);
done();
});
});
it('POST送信後にクッキーがセットされリダイレクト先に飛ぶ(絶対パス)', function (done) {
var url = helper.url('manual', 'euc-jp');
cli.fetch(helper.url('form', 'utf-8') + '?reset_trace_route', function (err, $, res, body) {
$('form[name=login]')
.submit(function (err, $, res, body) {
assert(typeOf(res.cookies) === 'object');
assert(res.cookies.user === 'guest');
assert($.documentInfo().url === url);
assert.deepEqual(JSON.parse(res.headers['trace-route']), [
'/form/utf-8.html?reset_trace_route',
'/~redirect',
'/manual/euc-jp.html'
]);
done();
});
});
});
it('POST送信後にクッキーがセットされリダイレクト先に飛ぶ(相対パス)', function (done) {
var url = helper.url('manual', 'euc-jp');
cli.fetch(helper.url('form', 'utf-8') + '?reset_trace_route', function (err, $, res, body) {
$('form[name=login]')
.attr('action', '/~redirect_relative')
.submit(function (err, $, res, body) {
assert(typeOf(res.cookies) === 'object');
assert(res.cookies.user === 'guest');
assert($.documentInfo().url === url);
assert.deepEqual(JSON.parse(res.headers['trace-route']), [
'/form/utf-8.html?reset_trace_route',
'/~redirect_relative',
'/manual/euc-jp.html'
]);
done();
});
});
});
});
describe('meta refresh', function () {
beforeEach(function () {
cli.set('followMetaRefresh', true);
});
it('meta[refresh]タグを検知してリダイレクト先に飛ぶ(絶対URL)', function (done) {
var url = helper.url('refresh', 'absolute');
cli.fetch(url, function (err, $, res, body) {
assert($.documentInfo().url === helper.url('~info'));
assert(res.headers.referer === url);
done();
});
});
it('meta[refresh]タグを検知してリダイレクト先に飛ぶ(相対URL)', function (done) {
var url = helper.url('refresh', 'relative');
cli.fetch(url, function (err, $, res, body) {
assert($.documentInfo().url === helper.url('~info'));
assert(res.headers.referer === url);
done();
});
});
it('followMetaRefresh:false => meta[refresh]タグがあってもリダイレクトしない', function (done) {
cli.set('followMetaRefresh', false);
var url = helper.url('refresh', 'absolute');
cli.fetch(url, function (err, $, res, body) {
assert($.documentInfo().url === url);
done();
});
});
it('IE条件コメント内のmeta[refresh]タグはリダイレクト対象外', function (done) {
var url = helper.url('refresh', 'ie-only');
cli.fetch(url, function (err, $, res, body) {
assert($.documentInfo().url === url);
assert($('title').text() === 'Refresh IE only');
done();
});
});
});
});
describe('sync', function () {
describe('30x', function () {
it('documentInfoにリダイレクト先のURLが登録される', function () {
var url = helper.url('manual', 'euc-jp');
var result = cli.fetchSync(helper.url('~redirect'));
assert(result.$.documentInfo().url === url);
});
it('POST送信後にクッキーがセットされリダイレクト先に飛ぶ', function (done) {
var url = helper.url('manual', 'euc-jp');
cli.fetch(helper.url('form', 'utf-8') + '?reset_trace_route', function (err, $, res, body) {
var result = $('form[name=login]').submitSync();
assert(typeOf(result.response.cookies) === 'object');
assert(result.response.cookies.user === 'guest');
assert(result.$.documentInfo().url === url);
assert.deepEqual(JSON.parse(result.response.headers['trace-route']), [
'/form/utf-8.html?reset_trace_route',
'/~redirect',
'/manual/euc-jp.html'
]);
done();
});
});
});
describe('meta refresh', function () {
beforeEach(function () {
cli.set('followMetaRefresh', true);
});
it('meta[refresh]タグを検知してリダイレクト先に飛ぶ(絶対URL)', function () {
var url = helper.url('refresh', 'absolute');
var result = cli.fetchSync(url);
assert(result.$.documentInfo().url === helper.url('~info'));
assert(result.response.headers.referer === url);
});
it('meta[refresh]タグを検知してリダイレクト先に飛ぶ(相対URL)', function () {
var url = helper.url('refresh', 'relative');
var result = cli.fetchSync(url);
assert(result.$.documentInfo().url === helper.url('~info'));
assert(result.response.headers.referer === url);
});
it('followMetaRefresh:false => meta[refresh]タグがあってもリダイレクトしない', function () {
cli.set('followMetaRefresh', false);
var url = helper.url('refresh', 'absolute');
var result = cli.fetchSync(url);
assert(result.$.documentInfo().url === url);
});
it('IE条件コメント内のmeta[refresh]タグはリダイレクト対象外', function () {
var url = helper.url('refresh', 'ie-only');
var result = cli.fetchSync(url);
assert(result.$.documentInfo().url === url);
assert(result.$('title').text() === 'Refresh IE only');
});
});
});
});