UNPKG

5.32 kBJavaScriptView Raw
1/*
2 * Copyright 2018 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12/* eslint-env mocha */
13const assert = require('assert');
14const winston = require('winston');
15const fetch = require('../src/html/fetch-markdown');
16
17const logger = winston.createLogger({
18 // tune this for debugging
19 level: 'debug',
20 // and turn this on if you want the output
21 silent: true,
22 format: winston.format.simple(),
23 transports: [new winston.transports.Console()],
24});
25
26describe('Test URI parsing and construction', () => {
27 it('fetch.uri is a function', () => {
28 assert.equal(typeof fetch.uri, 'function');
29 });
30
31 it('fetch.uri constructs URIs', () => {
32 assert.equal(
33 fetch.uri(
34 'https://raw.githubusercontent.com',
35 'adobe',
36 'xdm',
37 'master',
38 'README.md',
39 ),
40 'https://raw.githubusercontent.com/adobe/xdm/master/README.md',
41 );
42 });
43
44 it('fetch.uri deals with trailing slashes', () => {
45 assert.equal(
46 fetch.uri(
47 'https://raw.githubusercontent.com/',
48 'adobe',
49 'xdm',
50 'master',
51 'README.md',
52 ),
53 'https://raw.githubusercontent.com/adobe/xdm/master/README.md',
54 );
55 });
56
57 it('fetch.uri deals with leading slashes', () => {
58 assert.equal(
59 fetch.uri(
60 'https://raw.githubusercontent.com',
61 'adobe',
62 'xdm',
63 'master',
64 '/README.md',
65 ),
66 'https://raw.githubusercontent.com/adobe/xdm/master/README.md',
67 );
68 });
69
70 it('fetch.uri deals with slashes in refs', () => {
71 assert.equal(
72 fetch.uri(
73 'https://raw.githubusercontent.com',
74 'adobe',
75 'xdm',
76 'tags/release_1',
77 '/README.md',
78 ),
79 'https://raw.githubusercontent.com/adobe/xdm/tags/release_1/README.md',
80 );
81 });
82
83 it('fetch.uri deals with ugly slashes in refs', () => {
84 assert.equal(
85 fetch.uri(
86 'https://raw.githubusercontent.com',
87 'adobe',
88 'xdm',
89 '/tags/release_1/',
90 '/README.md',
91 ),
92 'https://raw.githubusercontent.com/adobe/xdm/tags/release_1/README.md',
93 );
94 });
95
96 it('fetch.uri deals with ugly slashes in owner', () => {
97 assert.equal(
98 fetch.uri(
99 'https://raw.githubusercontent.com',
100 '/adobe/',
101 'xdm',
102 'tags/release_1',
103 '/README.md',
104 ),
105 'https://raw.githubusercontent.com/adobe/xdm/tags/release_1/README.md',
106 );
107 });
108
109 it('fetch.uri deals with ugly slashes in repo', () => {
110 assert.equal(
111 fetch.uri(
112 'https://raw.githubusercontent.com',
113 'adobe',
114 '/xdm/',
115 'tags/release_1',
116 '/README.md',
117 ),
118 'https://raw.githubusercontent.com/adobe/xdm/tags/release_1/README.md',
119 );
120 });
121});
122
123describe('Test invalid input', () => {
124 it('Test for missing owner', () => {
125 assert.ok(fetch(
126 {},
127 {
128 request: { params: { repo: 'xdm', ref: 'master', path: 'README.md' } },
129 logger,
130 },
131 ).error);
132 });
133
134 it('Test for missing repo', () => {
135 assert.ok(fetch(
136 {},
137 {
138 request: { params: { ref: 'master', path: 'README.md', owner: 'adobe' } },
139 logger,
140 },
141 ).error);
142 });
143
144 it('Test for missing ref', () => {
145 assert.ok(fetch(
146 {},
147 {
148 request: { params: { repo: 'xdm', path: 'README.md', owner: 'adobe' } },
149 logger,
150 },
151 ).error);
152 });
153
154 it('Test for missing path', () => {
155 assert.ok(fetch(
156 {},
157 {
158 request: { params: { repo: 'xdm', ref: 'master', owner: 'adobe' } },
159 logger,
160 },
161 ).error);
162 });
163
164 it('Test for missing params', () => {
165 assert.ok(fetch(
166 {},
167 {
168 request: {},
169 logger,
170 },
171 ).error);
172 });
173
174 it('Test for missing request', () => {
175 assert.ok(fetch(
176 {},
177 { logger },
178 ).error);
179 });
180
181 it('Test for error pass-through', () => {
182 const err = { message: 'this error is mine.' };
183
184 assert.deepEqual(fetch(
185 { error: err },
186 { logger },
187 ), {});
188 });
189});
190
191describe('Test non-existing content', () => {
192 it('Getting XDM README (from wrong URL)', () => {
193 assert.ok(fetch(
194 {},
195 {
196 request: {
197 params: {
198 repo: 'xdm', ref: 'master', path: 'README.md', owner: 'nobody',
199 },
200 },
201 logger,
202 },
203 ).error);
204 });
205});
206
207describe('Test requests', () => {
208 it('Getting XDM README', (done) => {
209 const result = fetch(
210 {},
211 {
212 request: {
213 params: {
214 repo: 'xdm', ref: 'master', path: 'README.md', owner: 'adobe',
215 },
216 },
217 logger,
218 },
219 );
220 result.then((res) => {
221 assert.ok(res.content.body);
222 done();
223 });
224 });
225});