Skip to content

Commit

Permalink
🤖 TEST: Add httpclient streaming mocking (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Oct 1, 2022
1 parent d1ba441 commit 17a6713
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/fixtures/demo_next/app/controller/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ exports.urllib = function* () {
};
};

exports.streaming = async ctx => {
const url = 'http://' + ctx.host;
const response = await ctx.httpclient.request(url + '/mock_url', {
method: 'GET',
streaming: true,
});
ctx.status = response.status;
ctx.body = response.res;
};

exports.mockUrlGet = function* () {
this.body = 'url get';
};
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/demo_next/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ module.exports = function(app) {
app.get('/user', app.controller.user.get);
app.post('/user', app.controller.user.post);
app.post('/file', app.controller.file);

app.get('/streaming', 'home.streaming');
};
13 changes: 13 additions & 0 deletions test/mock_httpclient_next.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const pedding = require('pedding');
const path = require('path');
const fs = require('fs');
const request = require('supertest');
const assert = require('assert');
const { sleep } = require('../lib/utils');
Expand Down Expand Up @@ -91,6 +92,18 @@ describe('test/mock_httpclient_next.test.js', () => {
.expect(200);
});

it('should support on streaming', async () => {
app.mockHttpclient(url, 'get', {
data: fs.readFileSync(__filename),
});

const res = await request(server)
.get('/streaming')
.expect(200);
assert.match(res.body.toString(), /should support on streaming/);
assert.equal(res.body.toString(), fs.readFileSync(__filename, 'utf8'));
});

it('should mock url support multi method', async () => {
app.mockCsrf();
app.mockHttpclient(url, [ 'get', 'post' ], {
Expand Down

0 comments on commit 17a6713

Please sign in to comment.