Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors Axios unit tests #369

Merged
merged 2 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 21 additions & 32 deletions packages/zipkin-instrumentation-axiosjs/test/integrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('axios instrumentation - integration test', () => {
let apiPort;
let record;
let tracer;

before((done) => {
mockServer().then(server => {
apiServer = server;
Expand All @@ -22,53 +23,41 @@ describe('axios instrumentation - integration test', () => {
done();
});
});

after(() => {
apiServer.close();
});
const getClient = () => wrapAxios(axios, {tracer, serviceName, remoteServiceName});

beforeEach(() => {
record = sinon.spy();
const recorder = {record};
const ctxImpl = new ExplicitContext();
tracer = new Tracer({recorder, ctxImpl});
const recorder = {record};
tracer = new Tracer({ctxImpl, recorder});
});

const getClient = () => wrapAxios(axios, {tracer, serviceName, remoteServiceName});

it('should add headers to requests', done => {
tracer.scoped(() => {
const zipkinAxiosClient = getClient();
const urlPath = '/weather/wuhan';
const url = `http://${apiHost}:${apiPort}${urlPath}?index=10&count=300`;
zipkinAxiosClient
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@openzipkin/zipkin-js-champions in I think every integration test, we copy/pasted the same code which didn't actually test that headers were added!

.get(url)
.then(() => {
const annotations = record.args.map(args => args[0]);
const initialTraceId = annotations[0].traceId.traceId;
annotations.forEach(ann => expect(ann.traceId.traceId)
.to.equal(initialTraceId).and
.to.have.lengthOf(16));

expect(annotations[0].annotation.annotationType).to.equal('ServiceName');
expect(annotations[0].annotation.serviceName).to.equal('weather-app');

expect(annotations[1].annotation.annotationType).to.equal('Rpc');
expect(annotations[1].annotation.name).to.equal('GET');

expect(annotations[2].annotation.annotationType).to.equal('BinaryAnnotation');
expect(annotations[2].annotation.key).to.equal('http.path');
expect(annotations[2].annotation.value).to.equal(urlPath);

expect(annotations[3].annotation.annotationType).to.equal('ClientSend');

expect(annotations[4].annotation.annotationType).to.equal('ServerAddr');

expect(annotations[5].annotation.annotationType).to.equal('BinaryAnnotation');
expect(annotations[5].annotation.key).to.equal('http.status_code');
expect(annotations[5].annotation.value).to.equal('202');

expect(annotations[6].annotation.annotationType).to.equal('ClientRecv');
done();
});
zipkinAxiosClient.get(url).then(response => {
const annotations = record.args.map(args => args[0]);
const traceId = annotations[0].traceId

const requestHeaders = response.data;
expect(requestHeaders['x-b3-traceid']).to.equal(traceId.traceId);
expect(requestHeaders['x-b3-parentspanid']).to.not.exist;
expect(requestHeaders['x-b3-spanid']).to.equal(traceId.spanId);
expect(requestHeaders['x-b3-sampled']).to.equal('1');
expect(requestHeaders['x-b3-flags']).to.not.exist;
done();
});
});
});

it('should support request shorthand (defaults to GET)', done => {
tracer.scoped(() => {
const zipkinAxiosClient = getClient();
Expand Down
20 changes: 4 additions & 16 deletions packages/zipkin-instrumentation-axiosjs/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,16 @@ import express from 'express';
export const mockServer = () => new Promise(resolve => {
const api = express();
api.get('/weather/wuhan', (req, res) => {
res.status(202).json({
traceId: req.header('X-B3-TraceId'),
spanId: req.header('X-B3-SpanId')
});
res.status(202).json(req.headers);
});
api.get('/weather/beijing', (req, res) => {
res.status(202).json({
traceId: req.header('X-B3-TraceId'),
spanId: req.header('X-B3-SpanId')
});
res.status(202).json(req.headers);
});
api.get('/weather/securedTown', (req, res) => {
res.status(400).json({
traceId: req.header('X-B3-TraceId'),
spanId: req.header('X-B3-SpanId')
});
res.status(400).json(req.headers);
});
api.get('/weather/bagCity', (req, res) => {
res.status(500).json({
traceId: req.header('X-B3-TraceId'),
spanId: req.header('X-B3-SpanId')
});
res.status(500).json(req.headers);
});
const apiServer = api.listen(0, () => {
resolve(apiServer);
Expand Down