Skip to content

Commit

Permalink
fix: Use streamingApiUrl to resolve streaming api
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Nov 21, 2021
1 parent ce80a50 commit 451203f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 4 deletions.
46 changes: 46 additions & 0 deletions src/http/base-http.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { SerializerNodejsImpl } from '../serializers';
import { BaseHttp } from './base-http';

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

describe('BaseHttp', () => {
it('creates header', () => {
const test = new Test();
expect(test.createHeader({ extra: 'header' })).toEqual({
Authorization: 'Bearer token',
'Content-Type': 'application/json',
extra: 'header',
});
});

it('override content-type header', () => {
const test = new Test();
expect(
test.createHeader({ 'Content-Type': 'multipart/form-data' }),
).toEqual({
Authorization: 'Bearer token',
'Content-Type': 'multipart/form-data',
});
});

it('resolves url', () => {
const test = new Test();
expect(test.resolveUrl('/api/v1/yay')).toEqual(
'https://mastodon.social/api/v1/yay',
);
});

it('resolves url with params', () => {
const test = new Test();
expect(test.resolveUrl('/api/v1/yay', { query: true })).toEqual(
'https://mastodon.social/api/v1/yay?query=true',
);
});
});
2 changes: 1 addition & 1 deletion src/http/base-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export abstract class BaseHttp implements Http {
);

return `${this.config.url}${path}${
searchParams !== '{}' ? `?${searchParams}` : ''
searchParams !== '' ? `?${searchParams}` : ''
}`;
}

Expand Down
34 changes: 34 additions & 0 deletions src/ws/base-ws.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { SerializerNodejsImpl } from '../serializers';
import { BaseWs } from './base-ws';

class Test extends BaseWs {
baseUrl = 'wss://mastodon.social';
config = {
url: 'https://mastodon.social',
accessToken: 'token',
};
serializer = new SerializerNodejsImpl();
version = '99.99.9';
stream = jest.fn();
}

describe('BaseWs', () => {
it('resolves url', () => {
const test = new Test();
expect(test.resolveUrl('/api/v1/streaming/public')).toBe(
'wss://mastodon.social/api/v1/streaming/public',
);
});

it('resolves url with params', () => {
const test = new Test();
expect(test.resolveUrl('/api/v1/streaming/public', { public: true })).toBe(
'wss://mastodon.social/api/v1/streaming/public?public=true',
);
});

it('resolves protocols', () => {
const test = new Test();
expect(test.createProtocols()).toEqual(['token']);
});
});
9 changes: 7 additions & 2 deletions src/ws/base-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Serializer } from '../serializers';
import { Ws, WsEvents } from './ws';

export abstract class BaseWs implements Ws {
protected abstract readonly baseUrl: string;
protected abstract readonly config: MastoConfig;
protected abstract readonly version: string;
protected abstract readonly serializer: Serializer;
Expand All @@ -14,7 +15,11 @@ export abstract class BaseWs implements Ws {
private supportsSecureToken() {
// Since v2.8.4, it is supported to pass access token with`Sec-Websocket-Protocol`
// https://github.com/tootsuite/mastodon/pull/10818
return this.version && semver.gte(this.version, '2.8.4', { loose: true });
return (
this.version &&
this.baseUrl.startsWith('wss:') &&
semver.gte(this.version, '2.8.4', { loose: true })
);
}

resolveUrl(path: string, params: Record<string, unknown> = {}) {
Expand All @@ -27,7 +32,7 @@ export abstract class BaseWs implements Ws {
params,
);

return this.config.url + path + (query !== '{}' ? `?${params}` : '');
return this.baseUrl + path + (query !== '' ? `?${query}` : '');
}

createProtocols(protocols = []) {
Expand Down
2 changes: 1 addition & 1 deletion src/ws/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './ws';
export * from './ws-impl';
export * from './ws-nodejs-impl';
export * from './ws-native-impl';
File renamed without changes.

0 comments on commit 451203f

Please sign in to comment.