-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor encoding and decoding to use same token logic and add unit t…
…ests
- Loading branch information
1 parent
d15597a
commit c44d71c
Showing
2 changed files
with
104 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { describe, expect, it, vi } from 'vitest'; | ||
|
||
import { codeServerRequest } from './data-encoder'; | ||
|
||
const settings = { | ||
codec: { | ||
endpoint: 'http://localcodecserver.com', | ||
passAccessToken: false, | ||
includeCredentials: false, | ||
}, | ||
}; | ||
|
||
describe('Codec Server Requests for Decode and Encode', () => { | ||
const payloads = { payloads: [{}] }; | ||
const namespace = 'test-namespace'; | ||
|
||
it('should send a request and return decoded payloads', async () => { | ||
global.fetch = vi.fn(() => | ||
Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve(payloads), | ||
} as Response), | ||
); | ||
|
||
const namespace = 'test-namespace'; | ||
const response = await codeServerRequest({ | ||
type: 'decode', | ||
payloads, | ||
namespace, | ||
settings, | ||
}); | ||
expect(response).toEqual(payloads); | ||
}); | ||
|
||
it('should return original payloads for decode on failure', async () => { | ||
global.fetch = vi.fn(() => | ||
Promise.resolve({ | ||
ok: false, | ||
status: 500, | ||
statusText: 'Internal Server Error', | ||
} as Response), | ||
); | ||
const response = await codeServerRequest({ | ||
type: 'decode', | ||
payloads, | ||
namespace, | ||
settings, | ||
}); | ||
expect(response).toEqual(payloads); | ||
}); | ||
|
||
it('should send a request and return encoded payloads', async () => { | ||
global.fetch = vi.fn(() => | ||
Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve(payloads), | ||
} as Response), | ||
); | ||
|
||
const response = await codeServerRequest({ | ||
type: 'encode', | ||
payloads, | ||
namespace, | ||
settings, | ||
}); | ||
expect(response).toEqual(payloads); | ||
}); | ||
|
||
it('should throw an error for encode on failure', async () => { | ||
global.fetch = vi.fn(() => | ||
Promise.resolve({ | ||
ok: false, | ||
status: 500, | ||
statusText: 'Internal Server Error', | ||
} as Response), | ||
); | ||
await expect( | ||
codeServerRequest({ type: 'encode', payloads, namespace, settings }), | ||
).rejects.toThrow(); | ||
}); | ||
}); |
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