@util.js/node-http 0.41.5 → 0.41.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +7 -3
  2. package/test/UrlsTest.js +0 -258
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "author": "Chris Topher <c@creemama.com>",
3
3
  "dependencies": {
4
- "@util.js/node-files": "0.41.5",
5
- "@util.js/promises": "0.41.5"
4
+ "@util.js/node-files": "0.41.7",
5
+ "@util.js/promises": "0.41.7"
6
6
  },
7
7
  "description": "JavaScript utility methods for URLs",
8
+ "files": [
9
+ "index.js",
10
+ "lib"
11
+ ],
8
12
  "keywords": [
9
13
  "uri",
10
14
  "uris",
@@ -27,5 +31,5 @@
27
31
  "type": "git",
28
32
  "url": "https://github.com/creemama/utiljs/tree/master/packages/utiljs-node-http"
29
33
  },
30
- "version": "0.41.5"
34
+ "version": "0.41.7"
31
35
  }
package/test/UrlsTest.js DELETED
@@ -1,258 +0,0 @@
1
- "use strict";
2
-
3
- const expect = require("chai").expect;
4
- const files = require("@util.js/node-files");
5
- const targetDir = files.join(__dirname, "..", "target");
6
- const urls = require("..");
7
- let fileNum = 0;
8
-
9
- describe("Urls", () => {
10
- describe("#(de|en)codeURI", () => {
11
- it("should operate as expected", () => {
12
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI
13
- const encoded = "https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B";
14
- const decoded = "https://mozilla.org/?x=шеллы";
15
- expect(urls.encodeURI(decoded)).to.eql(encoded);
16
- expect(urls.decodeURI(encoded)).to.eql(decoded);
17
- });
18
- });
19
-
20
- describe("#(de|en)codeURIComponent", () => {
21
- it("should operate as expected", () => {
22
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent
23
- const encoded0 = "JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B";
24
- const decoded0 = "JavaScript_шеллы";
25
- const encoded1 = "%D1%88%D0%B5%D0%BB%D0%BB%D1%8B";
26
- const decoded1 = "шеллы";
27
- expect(urls.decodeURIComponent(encoded0)).to.eql(decoded0);
28
- expect(urls.decodeURIComponent(encoded1)).to.eql(decoded1);
29
- expect(urls.encodeURIComponent(decoded0)).to.eql(encoded0);
30
- expect(urls.encodeURIComponent(decoded1)).to.eql(encoded1);
31
- });
32
- });
33
-
34
- describe("#download(url, destination)", () => {
35
- it("handles HTTP requests", () =>
36
- downloadUrlDestination("http://www.google.com"));
37
- it("handles HTTPS requests", () =>
38
- downloadUrlDestination("https://www.google.com"));
39
- it("should throw an error if the protocol is not HTTP or HTTPS", () =>
40
- downloadUrlDestination("ftp://www.google.com").then(
41
- (value) => {
42
- throw new Error("Unexpected success");
43
- },
44
- (error) => {
45
- expect(error).to.be.an.instanceof(TypeError);
46
- },
47
- ));
48
- it("should throw an error if there is no protocol", () =>
49
- downloadUrlDestination("www.google.com").then(
50
- (value) => {
51
- throw new Error("Unexpected success");
52
- },
53
- (error) => {
54
- expect(error).to.be.an.instanceof(TypeError);
55
- },
56
- ));
57
- it("handles HTTP errors", () =>
58
- downloadUrlDestination("http://doesnotexist:8080", {
59
- timeout: 50,
60
- }).then(
61
- (value) => {
62
- throw new Error("Unexpected success");
63
- },
64
- (error) => {
65
- if (!error) throw new Error("Unexpected");
66
- /* expected */
67
- },
68
- ));
69
- it("handles HTTPS errors", () =>
70
- downloadUrlDestination("https://doesnotexist:8443", {
71
- timeout: 50,
72
- }).then(
73
- (value) => {
74
- throw new Error("Unexpected success");
75
- },
76
- (error) => {
77
- if (!error) throw new Error("Unexpected");
78
- /* expected */
79
- },
80
- ));
81
- });
82
-
83
- describe("#download(url, destination, callback)", () => {
84
- it("handles HTTP requests", (callback) =>
85
- downloadUrlDestinationCallback("http://www.google.com", callback));
86
- it("handles HTTPS requests", (callback) =>
87
- downloadUrlDestinationCallback("https://www.google.com", callback));
88
- it("handles HTTP errors", (callback) =>
89
- downloadUrlDestinationCallback(
90
- "http://doesnotexist:8080",
91
- { timeout: 50 },
92
- (error) => {
93
- if (error) return callback();
94
- callback(new Error("Unexpected success"));
95
- },
96
- ));
97
- it("handles HTTPS errors", (callback) =>
98
- downloadUrlDestinationCallback(
99
- "https://doesnotexist:8443",
100
- { timeout: 50 },
101
- (error) => {
102
- if (error) return callback();
103
- callback(new Error("Unexpected success"));
104
- },
105
- ));
106
- });
107
-
108
- describe("#headers(url)", () => {
109
- it("handles HTTP requests", () =>
110
- urls.headers("http://www.google.com").then((headers) => {
111
- expect(headers["content-type"].startsWith("text/html")).to.be.true;
112
- }));
113
- it("handles HTTPS requests", () =>
114
- urls.headers("https://www.google.com").then((headers) => {
115
- expect(headers["content-type"].startsWith("text/html")).to.be.true;
116
- }));
117
- it("handles HTTPS requests with null options", () =>
118
- urls.headers("https://www.google.com", null).then((headers) => {
119
- expect(headers["content-type"].startsWith("text/html")).to.be.true;
120
- }));
121
- it("handles HTTP requests with explicit port", () =>
122
- urls.headers("http://www.google.com:80").then((headers) => {
123
- expect(headers["content-type"].startsWith("text/html")).to.be.true;
124
- }));
125
- it("handles HTTP requests with empty options object", () =>
126
- urls.headers("http://www.google.com", {}).then((headers) => {
127
- expect(headers["content-type"].startsWith("text/html")).to.be.true;
128
- }));
129
- it("handles HTTP errors", () =>
130
- urls.headers("http://doesnotexist:8080", { timeout: 50 }).then(
131
- (value) => {
132
- throw new Error("Unexpected success");
133
- },
134
- (error) => {
135
- if (!error) throw new Error("Unexpected");
136
- /* expected */
137
- },
138
- ));
139
- it("handles HTTPS errors", () =>
140
- urls.headers("https://doesnotexist:8443", { timeout: 50 }).then(
141
- (value) => {
142
- throw new Error("Unexpected success");
143
- },
144
- (error) => {
145
- if (!error) throw new Error("Unexpected");
146
- /* expected */
147
- },
148
- ));
149
- it("handles request timeouts", () =>
150
- urls.headers("http://10.255.255.1", { timeout: 10 }).then(
151
- (value) => {
152
- throw new Error("Unexpected success");
153
- },
154
- (error) => {
155
- expect(error).to.be.an.instanceof(Error);
156
- },
157
- ));
158
- });
159
-
160
- describe("#headers(url, callback)", () => {
161
- it("handles HTTP requests", (callback) =>
162
- urls.headers("http://www.google.com", (error, headers) => {
163
- expect(headers["content-type"].startsWith("text/html")).to.be.true;
164
- callback(error);
165
- }));
166
- it("handles HTTPS requests", (callback) =>
167
- urls.headers("https://www.google.com", (error, headers) => {
168
- expect(headers["content-type"].startsWith("text/html")).to.be.true;
169
- callback(error);
170
- }));
171
- it("handles HTTP requests with empty options object", (callback) =>
172
- urls.headers("http://www.google.com", {}, (error, headers) => {
173
- expect(headers["content-type"].startsWith("text/html")).to.be.true;
174
- callback(error);
175
- }));
176
- it("handles HTTP errors", (callback) =>
177
- urls.headers("http://doesnotexist:8080", { timeout: 50 }, (error) => {
178
- if (error) return callback();
179
- callback(new Error("Unexpected success"));
180
- }));
181
- it("handles HTTPS errors", (callback) =>
182
- urls.headers("https://doesnotexist:8443", { timeout: 50 }, (error) => {
183
- if (error) return callback();
184
- callback(new Error("Unexpected success"));
185
- }));
186
- it("handles request timeouts", (callback) =>
187
- urls.headers("http://10.255.255.1", { timeout: 10 }, (error) => {
188
- if (error) return callback();
189
- callback(new Error("Unexpected success"));
190
- }));
191
- });
192
- });
193
-
194
- async function downloadUrlDestination(url, options) {
195
- await files.mkdirp(targetDir);
196
- const destination =
197
- targetDir +
198
- "/" +
199
- files.sanitizeFilename(
200
- fileNum++ + ".downloadUrlDestination." + url + ".html",
201
- { replacement: "_" },
202
- );
203
-
204
- const readFile = (_) => files.readFile(destination, "utf8");
205
- const verifyFile = (text) =>
206
- expect(text.startsWith("<!doctype html>")).to.be.true;
207
-
208
- if (options)
209
- return await urls
210
- .download(url, destination, options)
211
- .then(readFile)
212
- .then(verifyFile);
213
- await urls.download(url, destination).then(readFile).then(verifyFile);
214
- return await urls
215
- .download(url, destination, null)
216
- .then(readFile)
217
- .then(verifyFile);
218
- }
219
-
220
- function downloadUrlDestinationCallback() {
221
- asyncDownloadUrlDestinationCallback(...arguments);
222
- }
223
-
224
- async function asyncDownloadUrlDestinationCallback(
225
- url,
226
- callbackOrOptions,
227
- callback,
228
- ) {
229
- await files.mkdirp(targetDir);
230
- const cb = arguments[arguments.length - 1];
231
- const destination =
232
- targetDir +
233
- "/" +
234
- files.sanitizeFilename(
235
- fileNum++ + ".asyncDownloadUrlDestinationCallback." + url + ".html",
236
- { replacement: "_" },
237
- );
238
- const options = callbackOrOptions
239
- ? typeof callbackOrOptions === "function"
240
- ? null
241
- : callbackOrOptions
242
- : null;
243
- await new Promise((resolve, reject) => {
244
- const downloadCallback = (error) => {
245
- if (error) return reject(error);
246
- resolve();
247
- };
248
- if (options != null)
249
- urls.download(url, destination, options, downloadCallback);
250
- else urls.download(url, destination, downloadCallback);
251
- })
252
- .then((_) => files.readFile(destination, "utf8"))
253
- .then((text) => {
254
- expect(text.startsWith("<!doctype html>")).to.be.true;
255
- cb();
256
- })
257
- .catch((error) => cb(error));
258
- }