yjryu / UI_Layout star
File name
Commit message
Commit date
yjryu 2024-01-10 b1cdf23 240110 류윤주 commit UNIX
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
'use strict';
var assert = require('assert');
var express = require('express');
var request = require('supertest');
var proxy = require('../');
describe('userResDecorator', function () {
  describe('when handling a 304', function () {
    this.timeout(10000);
    var app;
    var  slowTarget;
    var  serverReference;
    beforeEach(function () {
      app = express();
      slowTarget = express();
      slowTarget.use(function (req, res) { res.sendStatus(304); });
      serverReference = slowTarget.listen(12345);
    });
    afterEach(function () {
      serverReference.close();
    });
    it('skips any handling', function (done) {
      app.use('/proxy', proxy('http://127.0.0.1:12345', {
        userResDecorator: function (/*res*/) {
          throw new Error('expected to never get here because this step should be skipped for 304');
        }
      }));
      request(app)
        .get('/proxy')
        .expect(304)
        .end(done);
    });
  });
  it('has access to original response', function (done) {
    var app = express();
    app.use(proxy('https://httpbin.org', {
      userResDecorator: function (proxyResproxyResData) {
        assert(proxyRes.connection);
        assert(proxyRes.socket);
        assert(proxyRes.headers);
        assert(proxyRes.headers['content-type']);
        return proxyResData;
      }
    }));
    request(app).get('/').end(done);
  });
  it('works with promises', function (done) {
    var app = express();
    app.use(proxy('https://httpbin.org', {
      userResDecorator: function (proxyResproxyResData) {
        return new Promise(function (resolve) {
          proxyResData.funkyMessage = 'oi io oo ii';
          setTimeout(function () {
            resolve(proxyResData);
          }, 200);
        });
      }
    }));
    request(app)
      .get('/ip')
      .end(function (err, res) {
        if (err) { return done(err); }
        assert(res.body.funkyMessage = 'oi io oo ii');
        done();
      });
  });
  it('can modify the response data', function (done) {
    var app = express();
    app.use(proxy('https://httpbin.org', {
      userResDecorator: function (proxyResproxyResData) {
        proxyResData = JSON.parse(proxyResData.toString('utf8'));
        proxyResData.intercepted = true;
        return JSON.stringify(proxyResData);
      }
    }));
    request(app)
      .get('/ip')
      .end(function (err, res) {
        if (err) { return done(err); }
        assert(res.body.intercepted);
        done();
      });
  });
  it('can modify the response headers, [deviant case, supported by pass-by-reference atm]', function (done) {
    var app = express();
    app.use(proxy('https://httpbin.org', {
      userResDecorator: function (rspdatareqres) {
        res.set('x-wombat-alliance', 'mammels');
        res.set('content-type', 'wiki/wiki');
        return data;
      }
    }));
    request(app)
      .get('/ip')
      .end(function (err, res) {
        if (err) { return done(err); }
        assert(res.headers['content-type'] === 'wiki/wiki');
        assert(res.headers['x-wombat-alliance'] === 'mammels');
        done();
      });
  });
  it('can mutuate an html response', function (done) {
    var app = express();
    app.use(proxy('httpbin.org', {
      userResDecorator: function (rspdata) {
        data = data.toString().replace('Oh', '<strong>Hey</strong>');
        assert(data !== '');
        return data;
      }
    }));
    request(app)
      .get('/html')
      .end(function (err, res) {
        if (err) { return done(err); }
        assert(res.text.indexOf('<strong>Hey</strong>') > -1);
        done();
      });
  });
  it('can change the location of a redirect', function (done) {
    function redirectingServer(portorigin) {
      var app = express();
      app.get('/', function (req, res) {
        res.status(302);
        res.location(origin + '/proxied/redirect/url');
        res.send();
      });
      return app.listen(port);
    }
    var redirectingServerPort = 8012;
    var redirectingServerOrigin = ['http://localhost', redirectingServerPort].join(':');
    var server = redirectingServer(redirectingServerPort, redirectingServerOrigin);
    var proxyApp = express();
    var preferredPort = 3000;
    proxyApp.use(proxy(redirectingServerOrigin, {
      userResDecorator: function (rspdatareqres) {
        var proxyReturnedLocation = res.getHeaders ? res.getHeaders().location : res._headers.location;
        res.location(proxyReturnedLocation.replace(redirectingServerPort, preferredPort));
        return data;
      }
    }));
    request(proxyApp)
      .get('/')
      .expect(function (res) {
        res.headers.location.match(/localhost:3000/);
      })
      .end(function () {
        server.close();
        done();
      });
  });
});
describe('test userResDecorator on html response from github', function () {
  /*
     Github provided a unique situation where the encoding was different than
     utf-8 when we didn't explicitly ask for utf-8.  This test helped sort out
     the issue, and even though its a little too on the nose for a specific
     case, it seems worth keeping around to ensure we don't regress on this
     issue.
  */
  it('is able to read and manipulate the response', function (done) {
    this.timeout(15000);  // give it some extra time to get response
    var app = express();
    app.use(proxy('https://github.com/villadora/express-http-proxy', {
      userResDecorator: function (targetResponsedata) {
        data = data.toString().replace('DOCTYPE', 'WINNING');
        assert(data !== '');
        return data;
      }
    }));
    request(app)
      .get('/html')
      .end(function (err, res) {
        if (err) { return done(err); }
        assert(res.text.indexOf('WINNING') > -1);
        done();
      });
  });
});
X