Skip to content

Commit

Permalink
Use hot-patcher to patch request rather than using a singleton and se…
Browse files Browse the repository at this point in the history
…tter
  • Loading branch information
perry-mitchell committed Nov 6, 2018
1 parent 213b3c8 commit 7a3cba6
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 32 deletions.
17 changes: 11 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"homepage": "https://github.com/perry-mitchell/webdav-client#readme",
"dependencies": {
"axios": "^0.18.0",
"hot-patcher": "^0.5.0",
"merge": "^1.2.1",
"path-posix": "^1.0.0",
"url-join": "^4.0.0",
Expand Down
4 changes: 3 additions & 1 deletion source/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
function generateBasicAuthHeader(username, password) {
return "Basic " + Buffer.from(username + ":" + password).toString("base64");
const buffer = Buffer.from(`${username}:${password}`);
return `Basic ${buffer.toString("base64")}`;
}

function generateTokenAuthHeader(tokenInfo) {
return `${tokenInfo.token_type} ${tokenInfo.access_token}`;
}
Expand Down
13 changes: 11 additions & 2 deletions source/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
const { axios, setRequestMethod } = require("./request.js");
const { axios } = require("./request.js");
const { createClient } = require("./factory.js");
const { getPatcher } = require("./patcher.js");

/**
* @module WebDAV
*/
module.exports = {
/**
* Axios request library
* @type {Function}
* @memberof module:WebDAV
*/
axios,
createClient,
setRequestMethod
getPatcher
};
18 changes: 18 additions & 0 deletions source/patcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const HotPatcher = require("hot-patcher");

let __patcher = null;

/**
* Get the HotPatcher instance for patching internal methods
* @returns {HotPatcher} The internal HotPatcher instance
*/
function getPatcher() {
if (!__patcher) {
__patcher = new HotPatcher();
}
return __patcher;
}

module.exports = {
getPatcher
};
30 changes: 12 additions & 18 deletions source/request.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const axios = require("axios");
const { merge } = require("./merge.js");
const { getPatcher } = require("./patcher.js");

const SEP_PATH_POSIX = "__PATH_SEPARATOR_POSIX__";
const SEP_PATH_WINDOWS = "__PATH_SEPARATOR_WINDOWS__";

let requestMethod = axios;

/**
* Encode a path for use with WebDAV servers
* @param {String} path The path to encode
Expand Down Expand Up @@ -50,30 +49,25 @@ function prepareRequestOptions(requestOptions, methodOptions) {

/**
* Make a request
* This method can be patched by patching or plugging-in to the "request"
* item using {@link https://github.com/perry-mitchell/hot-patcher HotPatcher}.
* It uses {@link https://github.com/axios/axios Axios} by default.
* @param {RequestOptions} requestOptions Options for the request
* @see axios
* @returns {Promise.<Object>} A promise that resolves with a response object
*/
function request(requestOptions) {
return requestMethod(requestOptions);
}

/**
* Set the request method to use when making requests
* Defaults to `axios`. Setting it to `null` will reset it to `axios`.
* @param {Function} fn Function to use - should perform like `axios()`.
* @example
* const createClient = require("webdav");
* createClient.setRequestMethod(someMethod);
*/
function setRequestMethod(fn) {
requestMethod = fn || axios;
return getPatcher().patchInline(
"request",
options => {
return axios(options);
},
requestOptions
);
}

module.exports = {
axios,
encodePath,
prepareRequestOptions,
request,
setRequestMethod
request
};
4 changes: 2 additions & 2 deletions test/specs/getFileContents.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const path = require("path");
const fs = require("fs");
const bufferEquals = require("buffer-equals");
const setRequestMethod = require("../../dist/request.js").setRequestMethod;
const getPatcher = require("../../dist/index.js").getPatcher;

const SOURCE_BIN = path.resolve(__dirname, "../testContents/alrighty.jpg");
const SOURCE_TXT = path.resolve(__dirname, "../testContents/text document.txt");

describe("getFileContents", function() {
beforeEach(function() {
restoreFetch(); // use default
getPatcher().restore("request"); // use default
this.client = createWebDAVClient("http://localhost:9988/webdav/server", {
username: createWebDAVServer.test.username,
password: createWebDAVServer.test.password
Expand Down
6 changes: 3 additions & 3 deletions test/specs/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require("path");

const { createClient: createWebDAVClient, setRequestMethod } = require("../../dist/index.js");
const { createClient: createWebDAVClient, getPatcher } = require("../../dist/index.js");
const baseWebDAVServer = require("../server/index.js");

const createWebDAVServer = baseWebDAVServer.webdavClient;
Expand All @@ -18,11 +18,11 @@ function clean() {
}

function restoreFetch() {
setRequestMethod();
getPatcher().restore("request");
}

function returnFakeResponse(xml) {
setRequestMethod(function fakeRequest() {
getPatcher().patch("request", function fakeRequest() {
return Promise.resolve({
data: xml
});
Expand Down

0 comments on commit 7a3cba6

Please sign in to comment.