|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as sinon from 'sinon'; |
| 18 | +import { sendWithXhr } from '../../src/platform/browser/util'; |
| 19 | +import { ensureHeadersContain } from '../helper'; |
| 20 | + |
| 21 | +describe('util - browser', () => { |
| 22 | + let server: any; |
| 23 | + const body = ''; |
| 24 | + const url = ''; |
| 25 | + |
| 26 | + let onSuccessStub: sinon.SinonStub; |
| 27 | + let onErrorStub: sinon.SinonStub; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + onSuccessStub = sinon.stub(); |
| 31 | + onErrorStub = sinon.stub(); |
| 32 | + server = sinon.fakeServer.create(); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + server.restore(); |
| 37 | + sinon.restore(); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('when XMLHTTPRequest is used', () => { |
| 41 | + let expectedHeaders: Record<string,string>; |
| 42 | + beforeEach(()=>{ |
| 43 | + expectedHeaders = { |
| 44 | + // ;charset=utf-8 is applied by sinon.fakeServer |
| 45 | + 'Content-Type': 'application/json;charset=utf-8', |
| 46 | + 'Accept': 'application/json', |
| 47 | + } |
| 48 | + }); |
| 49 | + describe('and Content-Type header is set', () => { |
| 50 | + beforeEach(()=>{ |
| 51 | + const explicitContentType = { |
| 52 | + 'Content-Type': 'application/json', |
| 53 | + }; |
| 54 | + sendWithXhr(body, url, explicitContentType, onSuccessStub, onErrorStub); |
| 55 | + }); |
| 56 | + it('Request Headers should contain "Content-Type" header', done => { |
| 57 | + |
| 58 | + setTimeout(() => { |
| 59 | + const { requestHeaders } = server.requests[0]; |
| 60 | + ensureHeadersContain(requestHeaders, expectedHeaders); |
| 61 | + done(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + it('Request Headers should contain "Accept" header', done => { |
| 65 | + |
| 66 | + setTimeout(() => { |
| 67 | + const { requestHeaders } = server.requests[0]; |
| 68 | + ensureHeadersContain(requestHeaders, expectedHeaders); |
| 69 | + done(); |
| 70 | + }); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('and empty headers are set', () => { |
| 75 | + beforeEach(()=>{ |
| 76 | + const emptyHeaders = {}; |
| 77 | + sendWithXhr(body, url, emptyHeaders, onSuccessStub, onErrorStub); |
| 78 | + }); |
| 79 | + it('Request Headers should contain "Content-Type" header', done => { |
| 80 | + |
| 81 | + setTimeout(() => { |
| 82 | + const { requestHeaders } = server.requests[0]; |
| 83 | + ensureHeadersContain(requestHeaders, expectedHeaders); |
| 84 | + done(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + it('Request Headers should contain "Accept" header', done => { |
| 88 | + |
| 89 | + setTimeout(() => { |
| 90 | + const { requestHeaders } = server.requests[0]; |
| 91 | + ensureHeadersContain(requestHeaders, expectedHeaders); |
| 92 | + done(); |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | + describe('and custom headers are set', () => { |
| 97 | + let customHeaders: Record<string,string>; |
| 98 | + beforeEach(()=>{ |
| 99 | + customHeaders = { aHeader: 'aValue', bHeader: 'bValue' }; |
| 100 | + sendWithXhr(body, url, customHeaders, onSuccessStub, onErrorStub); |
| 101 | + }); |
| 102 | + it('Request Headers should contain "Content-Type" header', done => { |
| 103 | + |
| 104 | + setTimeout(() => { |
| 105 | + const { requestHeaders } = server.requests[0]; |
| 106 | + ensureHeadersContain(requestHeaders, expectedHeaders); |
| 107 | + done(); |
| 108 | + }); |
| 109 | + }); |
| 110 | + it('Request Headers should contain "Accept" header', done => { |
| 111 | + |
| 112 | + setTimeout(() => { |
| 113 | + const { requestHeaders } = server.requests[0]; |
| 114 | + ensureHeadersContain(requestHeaders, expectedHeaders); |
| 115 | + done(); |
| 116 | + }); |
| 117 | + }); |
| 118 | + it('Request Headers should contain custom headers', done => { |
| 119 | + |
| 120 | + setTimeout(() => { |
| 121 | + const { requestHeaders } = server.requests[0]; |
| 122 | + ensureHeadersContain(requestHeaders, customHeaders); |
| 123 | + done(); |
| 124 | + }); |
| 125 | + }); |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments