This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: record http client response data
- Loading branch information
Showing
5 changed files
with
147 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/hook/test/fixtures/http-client-record-response/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { RunUtil } from '../../RunUtil'; | ||
import * as assert from 'assert'; | ||
// 放在前面,把 http.ClientRequest 先复写 | ||
const nock = require('nock'); | ||
import { HttpServerPatcher, HttpClientPatcher } from '../../../src/'; | ||
const httpServerPatcher = new HttpServerPatcher(); | ||
const httpClientPatcher = new HttpClientPatcher({ | ||
// nock 复写了 https.request 方法,没有像原始一样调用 http.request,所以需要强制复写 | ||
forceHttps: true, | ||
recordResponse: true | ||
}); | ||
|
||
RunUtil.run(function(done) { | ||
httpServerPatcher.run(); | ||
httpClientPatcher.run(); | ||
|
||
const http = require('http'); | ||
const https = require('https'); | ||
|
||
process.on(<any>'PANDORA_PROCESS_MESSAGE_TRACE', (report: any) => { | ||
const spans = report.spans; | ||
assert(spans.length === 2); | ||
const logs = spans[1].logs; | ||
const fields = logs[0].fields; | ||
assert(fields[0].key === 'response'); | ||
assert(fields[0].value === 'Response from TaoBao.'); | ||
|
||
done(); | ||
}); | ||
|
||
nock('https://www.taobao.com') | ||
.get('/') | ||
.reply(200, 'Response from TaoBao.'); | ||
|
||
function request(agent, options) { | ||
|
||
return new Promise((resolve, reject) => { | ||
const req = agent.request(options, (res) => { | ||
let data = ''; | ||
|
||
res.on('data', (d) => { | ||
data += d; | ||
}); | ||
|
||
res.on('end', () => { | ||
resolve([res, data]); | ||
}); | ||
}); | ||
|
||
req.on('error', (e) => { | ||
reject(e); | ||
}); | ||
|
||
req.end(); | ||
}); | ||
} | ||
|
||
const server = http.createServer((req, res) => { | ||
request(https, { | ||
hostname: 'www.taobao.com', | ||
path: '/', | ||
method: 'GET' | ||
}).then((response) => { | ||
res.end('ok'); | ||
}); | ||
}); | ||
|
||
server.listen(0, () => { | ||
const port = server.address().port; | ||
|
||
setTimeout(function() { | ||
request(http, { | ||
hostname: 'localhost', | ||
port: port, | ||
path: '/', | ||
method: 'GET' | ||
}).catch((err) => { | ||
console.log('err: ', err); | ||
}); | ||
}, 500); | ||
}); | ||
}); | ||
|
8 changes: 8 additions & 0 deletions
8
packages/hook/test/fixtures/http-client-record-response/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "http-client-test", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"dependencies": { | ||
"urllib": "^2.25.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters