-
Notifications
You must be signed in to change notification settings - Fork 804
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2ecf3b
commit 0e75af3
Showing
5 changed files
with
271 additions
and
14 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
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
80 changes: 80 additions & 0 deletions
80
experimental/packages/otlp-exporter-base/test/browser/send-beacon-transport.test.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,80 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import * as sinon from 'sinon'; | ||
import { createSendBeaconTransport } from '../../src/platform/browser/send-beacon-transport'; | ||
import * as assert from 'assert'; | ||
|
||
describe('SendBeaconTransport', function () { | ||
afterEach(function () { | ||
sinon.restore(); | ||
}); | ||
|
||
describe('send', function () { | ||
it('returns failure when sendBeacon fails', async function () { | ||
// arrange | ||
const sendStub = sinon.stub(navigator, 'sendBeacon').returns(false); | ||
const transport = createSendBeaconTransport({ | ||
url: 'http://example.test', | ||
blobType: 'application/json', | ||
}); | ||
|
||
// act | ||
const result = await transport.send(Uint8Array.from([1, 2, 3]), 1000); | ||
|
||
// assert | ||
sinon.assert.calledOnceWithMatch( | ||
sendStub, | ||
'http://example.test', | ||
sinon.match | ||
.instanceOf(Blob) | ||
.and( | ||
sinon.match( | ||
actual => actual.type === 'application/json', | ||
'Expected Blob type to match.' | ||
) | ||
) | ||
); | ||
assert.strictEqual(result.status, 'failure'); | ||
}); | ||
|
||
it('returns success when sendBeacon succeeds', async function () { | ||
// arrange | ||
const sendStub = sinon.stub(navigator, 'sendBeacon').returns(true); | ||
const transport = createSendBeaconTransport({ | ||
url: 'http://example.test', | ||
blobType: 'application/json', | ||
}); | ||
|
||
// act | ||
const result = await transport.send(Uint8Array.from([1, 2, 3]), 1000); | ||
|
||
// assert | ||
sinon.assert.calledOnceWithMatch( | ||
sendStub, | ||
'http://example.test', | ||
sinon.match | ||
.instanceOf(Blob) | ||
.and( | ||
sinon.match( | ||
actual => actual.type === 'application/json', | ||
'Expected Blob type to match.' | ||
) | ||
) | ||
); | ||
assert.strictEqual(result.status, 'success'); | ||
}); | ||
}); | ||
}); |
181 changes: 181 additions & 0 deletions
181
experimental/packages/otlp-exporter-base/test/browser/xhr-transport.test.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,181 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as sinon from 'sinon'; | ||
import * as assert from 'assert'; | ||
import { createXhrTransport } from '../../src/platform/browser/xhr-transport'; | ||
import { | ||
ExportResponseRetryable, | ||
ExportResponseFailure, | ||
ExportResponseSuccess, | ||
} from '../../src'; | ||
import { ensureHeadersContain } from '../testHelper'; | ||
|
||
const testTransportParameters = { | ||
url: 'http://example.test', | ||
headers: { | ||
foo: 'foo-value', | ||
bar: 'bar-value', | ||
'Content-Type': 'application/json', | ||
}, | ||
}; | ||
|
||
const requestTimeout = 1000; | ||
const testPayload = Uint8Array.from([1, 2, 3]); | ||
|
||
describe('XhrTransport', function () { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
describe('send', function () { | ||
it('returns success when request succeeds', function (done) { | ||
// arrange | ||
const server = sinon.fakeServer.create(); | ||
const transport = createXhrTransport(testTransportParameters); | ||
|
||
let request: sinon.SinonFakeXMLHttpRequest; | ||
queueMicrotask(() => { | ||
// this executes after the act block | ||
request = server.requests[0]; | ||
request.respond(200, {}, 'test response'); | ||
}); | ||
|
||
//act | ||
transport.send(testPayload, requestTimeout).then(response => { | ||
// assert | ||
try { | ||
assert.strictEqual(response.status, 'success'); | ||
// currently we don't do anything with the response yet, so it's dropped by the transport. | ||
assert.strictEqual( | ||
(response as ExportResponseSuccess).data, | ||
undefined | ||
); | ||
assert.strictEqual(request.url, testTransportParameters.url); | ||
assert.strictEqual( | ||
(request.requestBody as unknown as Blob).type, | ||
'application/json' | ||
); | ||
ensureHeadersContain(request.requestHeaders, { | ||
foo: 'foo-value', | ||
bar: 'bar-value', | ||
// ;charset=utf-8 is applied by sinon.fakeServer | ||
'Content-Type': 'application/json;charset=utf-8', | ||
}); | ||
} catch (e) { | ||
done(e); | ||
} | ||
done(); | ||
}, done /* catch any rejections */); | ||
}); | ||
|
||
it('returns failure when request fails', function (done) { | ||
// arrange | ||
const server = sinon.fakeServer.create(); | ||
const transport = createXhrTransport(testTransportParameters); | ||
|
||
queueMicrotask(() => { | ||
// this executes after the act block | ||
const request = server.requests[0]; | ||
request.respond(404, {}, ''); | ||
}); | ||
|
||
//act | ||
transport.send(testPayload, requestTimeout).then(response => { | ||
// assert | ||
try { | ||
assert.strictEqual(response.status, 'failure'); | ||
} catch (e) { | ||
done(e); | ||
} | ||
done(); | ||
}, done /* catch any rejections */); | ||
}); | ||
|
||
it('returns retryable when request is retryable', function (done) { | ||
// arrange | ||
const server = sinon.fakeServer.create(); | ||
const transport = createXhrTransport(testTransportParameters); | ||
|
||
queueMicrotask(() => { | ||
// this executes after the act block | ||
const request = server.requests[0]; | ||
request.respond(503, { 'Retry-After': 5 }, ''); | ||
}); | ||
|
||
//act | ||
transport.send(testPayload, requestTimeout).then(response => { | ||
// assert | ||
try { | ||
assert.strictEqual(response.status, 'retryable'); | ||
assert.strictEqual( | ||
(response as ExportResponseRetryable).retryInMillis, | ||
5000 | ||
); | ||
} catch (e) { | ||
done(e); | ||
} | ||
done(); | ||
}, done /* catch any rejections */); | ||
}); | ||
|
||
it('returns failure when request times out', function (done) { | ||
// arrange | ||
// A fake server needed, otherwise the message will not be a timeout but a failure to connect. | ||
sinon.useFakeServer(); | ||
const clock = sinon.useFakeTimers(); | ||
const transport = createXhrTransport(testTransportParameters); | ||
|
||
//act | ||
transport.send(testPayload, requestTimeout).then(response => { | ||
// assert | ||
try { | ||
assert.strictEqual(response.status, 'failure'); | ||
assert.strictEqual( | ||
(response as ExportResponseFailure).error.message, | ||
'XHR request timed out' | ||
); | ||
} catch (e) { | ||
done(e); | ||
} | ||
done(); | ||
}, done /* catch any rejections */); | ||
clock.tick(requestTimeout + 100); | ||
}); | ||
|
||
it('returns failure when no server exists', function (done) { | ||
// arrange | ||
const clock = sinon.useFakeTimers(); | ||
const transport = createXhrTransport(testTransportParameters); | ||
|
||
//act | ||
transport.send(testPayload, requestTimeout).then(response => { | ||
// assert | ||
try { | ||
assert.strictEqual(response.status, 'failure'); | ||
assert.strictEqual( | ||
(response as ExportResponseFailure).error.message, | ||
'XHR request errored' | ||
); | ||
} catch (e) { | ||
done(e); | ||
} | ||
done(); | ||
}, done /* catch any rejections */); | ||
clock.tick(requestTimeout + 100); | ||
}); | ||
}); | ||
}); |