1 |
|
2 |
|
3 |
|
4 |
|
5 | 'use strict';
|
6 |
|
7 | const fs = require('fs');
|
8 | const Log = require('log');
|
9 | const HttpReader = require('../src/Readers/HttpReader');
|
10 | const eol = require('eol');
|
11 | const AbstractReader = require('../src/Readers/AbstractReader');
|
12 |
|
13 | describe('HttpReader', () => {
|
14 |
|
15 | let reader;
|
16 |
|
17 | beforeEach(function () {
|
18 | reader = new HttpReader();
|
19 | reader.timeout = 30000;
|
20 |
|
21 |
|
22 | reader.logger = new Log(process.env.SPEC_LOGLEVEL || 'error');
|
23 | });
|
24 |
|
25 | it('should read sample#1 from githubusercontent.com (http)', () => {
|
26 | const remote = reader.read('http://raw.githubusercontent.com/electricimp/Builder/master/spec/fixtures/sample-1/input.nut');
|
27 | const local = fs.readFileSync(__dirname + '/fixtures/sample-1/input.nut', 'utf-8');
|
28 | expect(remote).toEqual(eol.lf(local));
|
29 | });
|
30 |
|
31 | it('should read sample#1 from githubusercontent.com (https)', () => {
|
32 | const remote = reader.read('https://raw.githubusercontent.com/electricimp/Builder/master/spec/fixtures/sample-1/input.nut');
|
33 | const local = fs.readFileSync(__dirname + '/fixtures/sample-1/input.nut', 'utf-8');
|
34 | expect(remote).toEqual(eol.lf(local));
|
35 | });
|
36 |
|
37 | it('should throw a timeout error', () => {
|
38 | reader.timeout = 1;
|
39 | try {
|
40 | reader.read('https://raw.githubusercontent.com/electricimp/Builder/master/spec/fixtures/sample-1/input.nut');
|
41 | fail();
|
42 | } catch (e) {
|
43 | expect(e instanceof AbstractReader.Errors.SourceReadingError).toBeTruthy();
|
44 |
|
45 |
|
46 |
|
47 | }
|
48 | });
|
49 |
|
50 | it('should throw HTTP/404 error', () => {
|
51 | try {
|
52 | reader.read('https://raw.githubusercontent.com/electricimp/Builder/master/spec/fixtures/sample-1/input.nut___');
|
53 | fail();
|
54 | } catch (e) {
|
55 | expect(e instanceof AbstractReader.Errors.SourceReadingError).toBeTruthy();
|
56 |
|
57 |
|
58 |
|
59 | }
|
60 | });
|
61 |
|
62 | it('should throw an error on url with unknown domain', () => {
|
63 | try {
|
64 | reader.read('http://__unknowndomain__/');
|
65 | fail();
|
66 | } catch (e) {
|
67 | expect(e instanceof AbstractReader.Errors.SourceReadingError).toBeTruthy();
|
68 | expect(e.message.indexOf('Failed to fetch url "http://__unknowndomain__/":')).toEqual(0);
|
69 | }
|
70 | });
|
71 |
|
72 | });
|