UNPKG

2.9 kBJavaScriptView Raw
1// Copyright (c) 2016-2017 Electric Imp
2// This file is licensed under the MIT License
3// http://opensource.org/licenses/MIT
4
5'use strict';
6
7const fs = require('fs');
8const Log = require('log');
9const HttpReader = require('../src/Readers/HttpReader');
10const eol = require('eol');
11const AbstractReader = require('../src/Readers/AbstractReader');
12
13describe('HttpReader', () => {
14
15 let reader;
16
17 beforeEach(function () {
18 reader = new HttpReader();
19 reader.timeout = 30000; // 30s
20
21 // @see https://www.npmjs.com/package/log#log-levels
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; // 1 ms
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 // TODO: on node 6 this error message is different (considering it to be a node issue)
45 // expect(e.message).diffChars('Failed to fetch url "https://raw.githubusercontent.com/' +
46 // 'electricimp/Builder/master/spec/fixtures/sample-1/input.nut": timed out after 0.001s');
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 // TODO: on node 6 this error message is different (considering it to be a node issue)
57 // expect(e.message).diffChars('Failed to fetch url "https://raw.githubusercontent.com/' +
58 // 'electricimp/Builder/master/spec/fixtures/sample-1/input.nut___": HTTP/404');
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});