Skip to content

Commit

Permalink
fix(fetch): Normalize header to be lowercase
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Nov 25, 2022
1 parent 68c71bd commit f7fce11
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 1 addition & 3 deletions src/http/http-native-impl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { headerCase } from 'change-case';

import type { MastoConfig } from '../config';
import type { CreateErrorParams } from '../errors';
import { createError, MastoError } from '../errors';
Expand Down Expand Up @@ -92,7 +90,7 @@ export class HttpNativeImpl extends BaseHttp implements Http {
const result: Record<string, unknown> = {};
// eslint-disable-next-line unicorn/no-array-for-each
headers.forEach((value, key) => {
result[headerCase(key)] = value;
result[key.toLowerCase()] = value;
});
return result as Headers;
}
Expand Down
13 changes: 9 additions & 4 deletions src/paginator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@ import { BaseHttp } from './http/base-http';
import { Paginator } from './paginator';
import { SerializerNodejsImpl } from './serializers';

const request = jest.fn();
class Test extends BaseHttp {
config = {
url: 'https://mastodon.social',
accessToken: 'token',
};
request = jest.fn();
request = request;
serializer = new SerializerNodejsImpl();
}

describe('Paginator', () => {
afterEach(() => {
request.mockClear();
});

it('sends a request', async () => {
const http = new Test();
http.request.mockReturnValue({ headers: {} });
const paginator = new Paginator(http, '/v1/api/timelines', {
foo: 'bar',
});
await paginator.next();
expect(http.request.mock.calls[0][0]).toEqual({
expect(http.request).toBeCalledWith({
method: 'get',
url: '/v1/api/timelines',
params: { foo: 'bar' },
Expand All @@ -30,13 +35,13 @@ describe('Paginator', () => {
const http = new Test();
http.request.mockReturnValue({
headers: {
Link: '<https://mastodon.social/api/v1/timelines/home?max_id=109382006402042919>; rel="next", <https://mastodon.social/api/v1/timelines/home?min_id=109382039876197520>; rel="prev"',
link: '<https://mastodon.social/api/v1/timelines/home?max_id=109382006402042919>; rel="next", <https://mastodon.social/api/v1/timelines/home?min_id=109382039876197520>; rel="prev"',
},
});
const paginator = new Paginator(http, '/v1/api/timelines');
await paginator.next();
await paginator.next();
expect(http.request.mock.calls[1][0]).toEqual({
expect(http.request).toBeCalledWith({
method: 'get',
params: undefined,
url: '/api/v1/timelines/home?max_id=109382006402042919',
Expand Down
4 changes: 2 additions & 2 deletions src/paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export class Paginator<Params, Result>
});

this.nextUrl =
typeof response.headers?.Link === 'string'
? this.pluckNext(response.headers.Link)
typeof response.headers?.link === 'string'
? this.pluckNext(response.headers.link)
: undefined;

return {
Expand Down

0 comments on commit f7fce11

Please sign in to comment.