diff --git a/src/http/base-http.spec.ts b/src/http/base-http.spec.ts new file mode 100644 index 000000000..8e3eab650 --- /dev/null +++ b/src/http/base-http.spec.ts @@ -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', + ); + }); +}); diff --git a/src/http/base-http.ts b/src/http/base-http.ts index 3d73d06ab..11c904ed5 100644 --- a/src/http/base-http.ts +++ b/src/http/base-http.ts @@ -24,7 +24,7 @@ export abstract class BaseHttp implements Http { ); return `${this.config.url}${path}${ - searchParams !== '{}' ? `?${searchParams}` : '' + searchParams !== '' ? `?${searchParams}` : '' }`; } diff --git a/src/ws/base-ws.spec.ts b/src/ws/base-ws.spec.ts new file mode 100644 index 000000000..aac5ab5fe --- /dev/null +++ b/src/ws/base-ws.spec.ts @@ -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']); + }); +}); diff --git a/src/ws/base-ws.ts b/src/ws/base-ws.ts index 2d95e0afa..337f56e79 100644 --- a/src/ws/base-ws.ts +++ b/src/ws/base-ws.ts @@ -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; @@ -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 = {}) { @@ -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 = []) { diff --git a/src/ws/index.ts b/src/ws/index.ts index 083c7c4ff..973076771 100644 --- a/src/ws/index.ts +++ b/src/ws/index.ts @@ -1,3 +1,3 @@ export * from './ws'; -export * from './ws-impl'; +export * from './ws-nodejs-impl'; export * from './ws-native-impl'; diff --git a/src/ws/ws-impl.ts b/src/ws/ws-nodejs-impl.ts similarity index 100% rename from src/ws/ws-impl.ts rename to src/ws/ws-nodejs-impl.ts