test workings

This commit is contained in:
simojenki
2022-04-23 14:23:20 +10:00
parent d2f13416f6
commit 1b14b88fb4
6 changed files with 19 additions and 210 deletions

View File

@@ -11,56 +11,10 @@ export interface Http {
(config: AxiosRequestConfig): AxiosPromise<any>;
}
// 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<AxiosRequestConfig>) =>
// (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<RequestParams>): Http =>
(config: AxiosRequestConfig) => {
let toApply = {

View File

@@ -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";

View File

@@ -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,

View File

@@ -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 = {

View File

@@ -1,154 +1,6 @@
import {
import { http, } from "../src/http";
http2,
} 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", () => {

View File

@@ -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(