1+ /*
2+ * Licensed under the Apache License, Version 2.0 (the "License");
3+ * you may not use this file except in compliance with the License.
4+ * You may obtain a copy of the License at
5+ *
6+ * http://www.apache.org/licenses/LICENSE-2.0
7+ *
8+ * Unless required by applicable law or agreed to in writing, software
9+ * distributed under the License is distributed on an "AS IS" BASIS,
10+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+ * See the License for the specific language governing permissions and
12+ * limitations under the License.
13+ */
14+
15+ 'use strict' ;
16+
17+ const ApArchiveLoader = require ( '../../src/loaders/aparchiveloader' ) ;
18+ const HTTPArchiveLoader = require ( '../../src/loaders/httparchiveloader' ) ;
19+ const chai = require ( 'chai' ) ;
20+ const sinon = require ( 'sinon' ) ;
21+
22+ chai . should ( ) ;
23+ chai . use ( require ( 'chai-as-promised' ) ) ;
24+
25+ describe ( 'ApArchiveLoader' , ( ) => {
26+
27+ let apLoader ;
28+ let httpLoadStub ;
29+
30+ beforeEach ( ( ) => {
31+ apLoader = new ApArchiveLoader ( ) ;
32+ // STUNT DOUBLE: We stub the parent class's load method.
33+ // We don't want to make a real HTTP request; we just want to know
34+ // what URL the ApLoader TRIED to fetch.
35+ httpLoadStub = sinon . stub ( HTTPArchiveLoader . prototype , 'load' ) . resolves ( 'ARCHIVE_DATA' ) ;
36+ } ) ;
37+
38+ afterEach ( ( ) => {
39+ // Restore the original function after every test so we don't break other tests
40+ httpLoadStub . restore ( ) ;
41+ } ) ;
42+
43+ describe ( '#accepts' , ( ) => {
44+ it ( 'should accept ap:// URLs' , ( ) => {
45+ apLoader . accepts ( 'ap://helloworld@0.2.0#hash' ) . should . be . true ;
46+ } ) ;
47+
48+ it ( 'should reject non-ap URLs' , ( ) => {
49+ apLoader . accepts ( 'http://google.com' ) . should . be . false ;
50+ apLoader . accepts ( 'https://templates.accordproject.org' ) . should . be . false ;
51+ } ) ;
52+ } ) ;
53+
54+ describe ( '#load' , ( ) => {
55+ it ( 'should rewrite the URL and delegate to HTTP load' , async ( ) => {
56+ const inputUrl = 'ap://helloworld@0.2.0#97886fa' ;
57+ // The logic in source says: substring(5, atIndex) -> "helloworld"
58+ // substring(atIndex+1, hashIndex) -> "0.2.0"
59+ // Result should be: https://templates.accordproject.org/archives/helloworld@0.2.0.cta
60+ const expectedUrl = 'https://templates.accordproject.org/archives/helloworld@0.2.0.cta' ;
61+
62+ const result = await apLoader . load ( inputUrl , { option : 'test' } ) ;
63+ result . should . equal ( 'ARCHIVE_DATA' ) ;
64+ // ASSERT: Did we transform the URL correctly?
65+ sinon . assert . calledWith ( httpLoadStub , expectedUrl , { option : 'test' } ) ;
66+ } ) ;
67+
68+ it ( 'should throw error if @ is missing' , async ( ) => {
69+ const inputUrl = 'ap://helloworld#hash' ;
70+ ( ( ) => {
71+ apLoader . load ( inputUrl , { } ) ;
72+ } ) . should . throw ( / I n v a l i d t e m p l a t e s p e c i f i e r / ) ;
73+ } ) ;
74+
75+ it ( 'should throw error if # is missing' , async ( ) => {
76+ const inputUrl = 'ap://helloworld@0.2.0' ;
77+ ( ( ) => {
78+ apLoader . load ( inputUrl , { } ) ;
79+ } ) . should . throw ( / I n v a l i d t e m p l a t e s p e c i f i e r / ) ;
80+ } ) ;
81+ } ) ;
82+ } ) ;
0 commit comments