From 1b14b88fb4840e3ebdf82a26657eee0756de1e09 Mon Sep 17 00:00:00 2001 From: simojenki Date: Sat, 23 Apr 2022 14:23:20 +1000 Subject: [PATCH] test workings --- src/http.ts | 48 +--------- src/subsonic/http.ts | 3 +- src/subsonic/index.ts | 3 +- src/subsonic/library.ts | 3 +- tests/http.test.ts | 170 +++------------------------------ tests/subsonic/generic.test.ts | 2 +- 6 files changed, 19 insertions(+), 210 deletions(-) diff --git a/src/http.ts b/src/http.ts index 8801df9..fbc9f66 100644 --- a/src/http.ts +++ b/src/http.ts @@ -11,56 +11,10 @@ export interface Http { (config: AxiosRequestConfig): AxiosPromise; } -// export const http = -// (base: Http = axios, modifier: RequestModifier = no_op): Http => -// (config: AxiosRequestConfig) => { -// console.log( -// `applying ${JSON.stringify(config)} onto ${JSON.stringify(modifier)}` -// ); -// const result = modifier(config); -// console.log(`result is ${JSON.stringify(result)}`); -// return base(result); -// }; - -// export const chain = -// (...modifiers: RequestModifier[]): RequestModifier => -// (config: AxiosRequestConfig) => -// modifiers.reduce( -// (config: AxiosRequestConfig, next: RequestModifier) => next(config), -// config -// ); - -// export const baseUrl = (baseURL: string) => (config: AxiosRequestConfig) => ({ -// ...config, -// baseURL, -// }); - -// export const axiosConfig = -// (additionalConfig: Partial) => -// (config: AxiosRequestConfig) => ({ ...config, ...additionalConfig }); - -// export const params = (params: any) => (config: AxiosRequestConfig) => { -// console.log( -// `params on config ${JSON.stringify( -// config.params -// )}, params applying ${JSON.stringify(params)}` -// ); -// const after = { ...config, params: { ...config.params, ...params } }; -// console.log(`params after ${JSON.stringify(after.params)}`); -// return after; -// }; - -// export const headers = (headers: any) => (config: AxiosRequestConfig) => ({ -// ...config, -// headers: { ...config.headers, ...headers }, -// }); -// export const formatJson = (): RequestModifier => (config: AxiosRequestConfig) => ({...config, params: { ...config.params, f: 'json' } }); -// export const subsonicAuth = (credentials: { username: string, password: string}) => (config: AxiosRequestConfig) => ({...config, params: { ...config.params, u: credentials.username, ...t_and_s(credentials.password) } }); - export type RequestParams = { baseURL: string; url: string, params: any, headers: any, responseType: ResponseType } // todo: rename to http -export const http2 = +export const http = (base: Http, defaults: Partial): Http => (config: AxiosRequestConfig) => { let toApply = { diff --git a/src/subsonic/http.ts b/src/subsonic/http.ts index 47b1d7c..57acfdb 100644 --- a/src/subsonic/http.ts +++ b/src/subsonic/http.ts @@ -6,7 +6,8 @@ import { t_and_s, USER_AGENT, } from "."; -import { Http, http2 } from "../http"; +// todo: rename http2 to http +import { Http, http as http2 } from "../http"; import { Credentials } from "../music_service"; import { asURLSearchParams } from "../utils"; diff --git a/src/subsonic/index.ts b/src/subsonic/index.ts index 3aee9f0..ffaf7a4 100644 --- a/src/subsonic/index.ts +++ b/src/subsonic/index.ts @@ -4,7 +4,8 @@ import { Md5 } from "ts-md5/dist/md5"; import axios from "axios"; import randomstring from "randomstring"; import _ from "underscore"; -import { Http, http2 } from "../http"; +// todo: rename http2 to http +import { Http, http as http2 } from "../http"; import { Credentials, diff --git a/src/subsonic/library.ts b/src/subsonic/library.ts index 95de93e..15873b8 100644 --- a/src/subsonic/library.ts +++ b/src/subsonic/library.ts @@ -37,7 +37,8 @@ import Subsonic, { import axios from "axios"; import { asURLSearchParams } from "../utils"; import { artistSummaryFromNDArtist, NDArtist } from "./navidrome"; -import { Http, http2 } from "../http"; +//todo: rename http2 -> http +import { Http, http as http2 } from "../http"; import { getRaw2 } from "./http"; type album = { diff --git a/tests/http.test.ts b/tests/http.test.ts index 5b0f595..85f69c6 100644 --- a/tests/http.test.ts +++ b/tests/http.test.ts @@ -1,154 +1,6 @@ -import { - - http2, -} from "../src/http"; +import { http, } from "../src/http"; -// describe("request modifiers", () => { -// describe("baseUrl", () => { -// it.each([ -// [ -// { data: "bob" }, -// "http://example.com", -// { data: "bob", baseURL: "http://example.com" }, -// ], -// [ -// { baseURL: "http://originalBaseUrl.example.com" }, -// "http://example.com", -// { baseURL: "http://example.com" }, -// ], -// ])( -// "should apply the baseUrl", -// (requestConfig: any, value: string, expected: any) => { -// expect(baseUrl(value)(requestConfig)).toEqual(expected); -// } -// ); -// }); - -// describe("params", () => { -// it.each([ -// [ -// { data: "bob" }, -// { param1: "value1", param2: "value2" }, -// { data: "bob", params: { param1: "value1", param2: "value2" } }, -// ], -// [ -// { data: "bob", params: { orig1: "origValue1" } }, -// {}, -// { data: "bob", params: { orig1: "origValue1" } }, -// ], -// [ -// { data: "bob", params: { orig1: "origValue1" } }, -// { param1: "value1", param2: "value2" }, -// { -// data: "bob", -// params: { orig1: "origValue1", param1: "value1", param2: "value2" }, -// }, -// ], -// ])( -// "should apply the params", -// (requestConfig: any, newParams: any, expected: any) => { -// expect(params(newParams)(requestConfig)).toEqual(expected); -// } -// ); -// }); - -// describe("headers", () => { -// it.each([ -// [ -// { data: "bob" }, -// { h1: "value1", h2: "value2" }, -// { data: "bob", headers: { h1: "value1", h2: "value2" } }, -// ], -// [ -// { data: "bob", headers: { orig1: "origValue1" } }, -// {}, -// { data: "bob", headers: { orig1: "origValue1" } }, -// ], -// [ -// { data: "bob", headers: { orig1: "origValue1" } }, -// { h1: "value1", h2: "value2" }, -// { -// data: "bob", -// headers: { orig1: "origValue1", h1: "value1", h2: "value2" }, -// }, -// ], -// ])( -// "should apply the headers", -// (requestConfig: any, newParams: any, expected: any) => { -// expect(headers(newParams)(requestConfig)).toEqual(expected); -// } -// ); -// }); - -// describe("chain", () => { -// it.each([ -// [ -// { data: "bob" }, -// [params({ param1: "value1", param2: "value2" })], -// { data: "bob", params: { param1: "value1", param2: "value2" } }, -// ], -// [ -// { data: "bob" }, -// [params({ param1: "value1" }), params({ param2: "value2" })], -// { data: "bob", params: { param1: "value1", param2: "value2" } }, -// ], -// [{ data: "bob" }, [], { data: "bob" }], -// ])( -// "should apply the chain", -// (requestConfig: any, newParams: RequestModifier[], expected: any) => { -// expect(chain(...newParams)(requestConfig)).toEqual(expected); -// } -// ); -// }); - -// describe("wrapping", () => { -// const mockAxios = jest.fn(); - -// describe("baseURL", () => { -// const base = http( -// mockAxios, -// baseUrl("http://original.example.com") -// ); - -// describe("when no baseURL passed in when being invoked", () => { -// it("should use the original value", () => { -// base({}) -// expect(mockAxios).toHaveBeenCalledWith({ baseURL: "http://original.example.com" }); -// }); -// }); - -// describe("when a new baseURL is passed in when being invoked", () => { -// it("should use the new value", () => { -// base({ baseURL: "http://new.example.com" }) -// expect(mockAxios).toHaveBeenCalledWith({ baseURL: "http://new.example.com" }); -// }); -// }); -// }); - -// describe("params", () => { -// const base = http( -// mockAxios, -// params({ a: "1", b: "2" }) -// ); - -// it("should apply the modified when invoked", () => { -// base({ method: 'get' }); -// expect(mockAxios).toHaveBeenCalledWith({ method: 'get', params: { a: "1", b: "2" }}); -// }); - -// describe("wrapping the base", () => { -// const wrapped = http(base, params({ b: "2b", c: "3" })); - -// it("should the wrapped values as priority", () => { -// wrapped({ method: 'get', params: { a: "1b", c: "3b", d: "4" } }); -// expect(mockAxios).toHaveBeenCalledWith({ method: 'get', params: { a: "1b", b: "2b", c: "3b", d: "4" }}); -// }); -// }); -// }); -// }); -// }); - -describe("http2", () => { +describe("http", () => { const mockAxios = jest.fn(); beforeEach(() => { @@ -166,7 +18,7 @@ describe("http2", () => { return thing; }; - const base = http2(mockAxios, getValue('base')); + const base = http(mockAxios, getValue('base')); describe("using default", () => { it("should use the default", () => { @@ -183,8 +35,8 @@ describe("http2", () => { }); describe("wrapping", () => { - const firstLayer = http2(base, getValue('level1')); - const secondLayer = http2(firstLayer, getValue('level2')); + const firstLayer = http(base, getValue('level1')); + const secondLayer = http(firstLayer, getValue('level2')); describe("when the outter call provides a value", () => { it("should apply it", () => { @@ -203,7 +55,7 @@ describe("http2", () => { }); describe("requestType", () => { - const base = http2(mockAxios, { responseType: 'stream' }); + const base = http(mockAxios, { responseType: 'stream' }); describe("using default", () => { it("should use the default", () => { @@ -220,8 +72,8 @@ describe("http2", () => { }); describe("wrapping", () => { - const firstLayer = http2(base, { responseType: 'arraybuffer' }); - const secondLayer = http2(firstLayer, { responseType: 'blob' }); + const firstLayer = http(base, { responseType: 'arraybuffer' }); + const secondLayer = http(firstLayer, { responseType: 'blob' }); describe("when the outter call provides a value", () => { it("should apply it", () => { @@ -248,7 +100,7 @@ describe("http2", () => { thing[field] = values; return thing; } - const base = http2(mockAxios, getValues({ a: 1, b: 2, c: 3, d: 4 })); + const base = http(mockAxios, getValues({ a: 1, b: 2, c: 3, d: 4 })); describe("using default", () => { it("should use the default", () => { @@ -265,8 +117,8 @@ describe("http2", () => { }); describe("wrapping", () => { - const firstLayer = http2(base, getValues({ b: 22 })); - const secondLayer = http2(firstLayer, getValues({ c: 33 })); + const firstLayer = http(base, getValues({ b: 22 })); + const secondLayer = http(firstLayer, getValues({ c: 33 })); describe("when the outter call provides a value", () => { it("should apply it", () => { diff --git a/tests/subsonic/generic.test.ts b/tests/subsonic/generic.test.ts index f99c6d5..3654c41 100644 --- a/tests/subsonic/generic.test.ts +++ b/tests/subsonic/generic.test.ts @@ -46,7 +46,7 @@ import { import { EMPTY, error, FAILURE, subsonicOK, ok } from "../subsonic.test"; import Subsonic, { DODGY_IMAGE_NAME, t } from "../../src/subsonic"; import { b64Encode } from "../../src/b64"; -import { http2 } from "../../src/http"; +import { http as http2 } from "../../src/http"; const maybeIdFromCoverArtUrn = (coverArt: BUrn | undefined) => pipe(