Skip to content

Commit

Permalink
refactor!: make the user agent optional
Browse files Browse the repository at this point in the history
user agent header needs to be optional because the API is returning
wrong CORS preflight request allowed headers
segler-alex/radiobrowser-api-rust#86
  • Loading branch information
ivandotv committed Nov 3, 2020
1 parent f82e5b1 commit c480a2b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
6 changes: 4 additions & 2 deletions src/radioBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ export class RadioBrowserApi {
}

constructor(
protected userAgent: string,
protected fetchImpl: typeof fetch,
protected userAgent?: string,
hideBroken = true
) {
this.fetchConfig.headers = { 'user-agent': this.userAgent }
if (this.userAgent) {
this.fetchConfig.headers = { 'user-agent': this.userAgent }
}
this.hideBroken = hideBroken
}

Expand Down
46 changes: 23 additions & 23 deletions tests/radioBrowser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ describe('Radio Browser', () => {
.get('/json/servers')
.reply(200, mockResult)

const api = new RadioBrowserApi('test', globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch)
const result = await api.resolveBaseUrl()

expect(result).toEqual(mockResult)
expect(api.getBaseUrl()).toBe('https://' + mockResult[0].name)
})

test('Resolve base url but do not set it', async () => {
const api = new RadioBrowserApi('test', globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch)
const defaultBaseUrl = api.getBaseUrl()
const mockResult = [
{
Expand All @@ -62,7 +62,7 @@ describe('Radio Browser', () => {
test('Manually set base url', () => {
const url = '1.2.3.4'

const api = new RadioBrowserApi('test', globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch)
api.setBaseUrl(url)

expect(api.getBaseUrl()).toBe(url)
Expand All @@ -74,7 +74,7 @@ describe('Radio Browser', () => {
.get('/json/servers')
.reply(500, errorText)

const api = new RadioBrowserApi('test', globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch)
expect.assertions(1)
try {
await api.resolveBaseUrl()
Expand All @@ -91,7 +91,7 @@ describe('Radio Browser', () => {

const agent = 'test'
const spy = jest.spyOn(globalTest, 'fetch')
const api = new RadioBrowserApi(agent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, agent)

await api.resolveBaseUrl()

Expand All @@ -108,7 +108,7 @@ describe('Radio Browser', () => {
.get('/json/servers')
.reply(200, [{ name: '', ip: '' }])

const api = new RadioBrowserApi('test', globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch)
const spy = jest.spyOn(globalTest, 'fetch')

const headerName = 'x-jest-test'
Expand All @@ -130,7 +130,7 @@ describe('Radio Browser', () => {

test('get countries', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('Radio Browser', () => {

test('get country by country code', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('Radio Browser', () => {

test('get codecs', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -225,7 +225,7 @@ describe('Radio Browser', () => {

test('get country states', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -258,7 +258,7 @@ describe('Radio Browser', () => {

test('get languages', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -291,7 +291,7 @@ describe('Radio Browser', () => {

test('get tags', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -324,7 +324,7 @@ describe('Radio Browser', () => {
describe('Get stations by', () => {
test('by language', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -362,7 +362,7 @@ describe('Radio Browser', () => {

test('by tag', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -401,7 +401,7 @@ describe('Radio Browser', () => {
expect.assertions(1)

const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)
try {
await api.getStationsBy(
// @ts-ignore
Expand All @@ -416,7 +416,7 @@ describe('Radio Browser', () => {

test('get all stations', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -448,7 +448,7 @@ describe('Radio Browser', () => {

test('send station click', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -476,7 +476,7 @@ describe('Radio Browser', () => {

test('vote for station', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -504,7 +504,7 @@ describe('Radio Browser', () => {

test('get stations by votes', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -538,7 +538,7 @@ describe('Radio Browser', () => {
// advanced station search
test('by tag list', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -579,7 +579,7 @@ describe('Radio Browser', () => {
// advanced station search
test('hide broken stations by default', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -619,7 +619,7 @@ describe('Radio Browser', () => {

test('hide broken stations explicitly', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down Expand Up @@ -656,7 +656,7 @@ describe('Radio Browser', () => {
})
test('Show broken stations', async () => {
const userAgent = 'test'
const api = new RadioBrowserApi(userAgent, globalTest.fetch)
const api = new RadioBrowserApi(globalTest.fetch, userAgent)

const headerName = 'x-jest-test'
const headerValue = '1'
Expand Down

0 comments on commit c480a2b

Please sign in to comment.