-
Notifications
You must be signed in to change notification settings - Fork 36
/
mock.ts
155 lines (146 loc) · 5.53 KB
/
mock.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* Copyright 2022 Inrupt Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import * as crossFetch from "cross-fetch";
const { Response } = crossFetch;
import {
Url,
UrlString,
SolidDataset,
File,
WithServerResourceInfo,
} from "../interfaces";
import { internal_toIriString } from "../interfaces.internal";
import { getSolidDataset, createSolidDataset } from "./solidDataset";
import { getFile } from "./file";
import { FetchError } from "./resource";
type Unpromisify<T> = T extends Promise<infer R> ? R : T;
/**
* ```{warning}
* Do not use this function in production code. For use in **unit tests**.
* ```
*
* This function initialises a new [[SolidDataset]] with metadata as though the
* SolidDataset has been retrieved from the given URL. The mock SolidDataset can be used in
* unit tests that require persisted SolidDatasets; e.g., unit tests that call [[getSourceUrl]].
*
* @param url The URL from which the returned SolidDataset appears to be retrieved.
* @returns A mock SolidDataset that appears to be retrieved from the `url`.
* @since 0.2.0
*/
export function mockSolidDatasetFrom(
url: Url | UrlString
): Unpromisify<ReturnType<typeof getSolidDataset>> {
const solidDataset = createSolidDataset();
const solidDatasetWithResourceInfo: SolidDataset & WithServerResourceInfo = {
...solidDataset,
internal_resourceInfo: {
sourceIri: internal_toIriString(url),
isRawData: false,
contentType: "text/turtle",
linkedResources: {},
},
};
return solidDatasetWithResourceInfo;
}
/**
* ```{warning}
* Do not use this function in production code. For use in **unit tests**.
* ```
*
* This function initialises a new Container [[SolidDataset]] with metadata as though the
* Container has been retrieved from the given URL. The mock SolidDataset can be used in
* unit tests that require persisted Containers; e.g., unit tests that call [[isContainer]].
*
* @param url The URL from which the returned Container appears to be retrieved. The `url` must end in a slash.
* @returns A mock SolidDataset that appears to be retrieved from the `url`.
* @since 0.2.0
*/
export function mockContainerFrom(
url: Url | UrlString
): Unpromisify<ReturnType<typeof getSolidDataset>> {
const sourceIri = internal_toIriString(url);
if (!sourceIri.endsWith("/")) {
throw new Error(
"A Container's URL should end in a slash. Please update your tests."
);
}
return mockSolidDatasetFrom(sourceIri);
}
/**
* ```{warning}
* Do not use this function in production code. For use in **unit tests**.
* ```
*
* This function initialises a new File with metadata as though the
* File has been retrieved from the given URL. The mock File can be used in
* unit tests that require persisted Files; e.g. unit tests that call [[getSourceUrl]].
*
* @param url The URL from which the returned File appears to be retrieved.
* @returns A mock File that appears to be retrieved from the `url`.
* @since 0.2.0
*/
export function mockFileFrom(
url: Url | UrlString,
options?: Partial<{
contentType: WithServerResourceInfo["internal_resourceInfo"]["contentType"];
}>
): Unpromisify<ReturnType<typeof getFile>> {
const file = new Blob();
const fileWithResourceInfo: File & WithServerResourceInfo = Object.assign(
file,
{
internal_resourceInfo: {
sourceIri: internal_toIriString(url),
isRawData: true,
contentType: options?.contentType,
linkedResources: {},
},
}
);
return fileWithResourceInfo;
}
/**
* ```{warning}
* Do not use this function in production code. For use in **unit tests**.
* ```
*
* This function initialises a new Error object with metadata as though the
* it was the result of getting a 404 when trying to fetch the Resource at the
* given URL. The mock Error can be used in unit tests that require functions
* that fetch Resources (like [[getSolidDataset]]) to fail.
*
* @param url The URL of the Resource that could not be fetched according to the error.
* @param statusCode Optional status code (defaults to 404) that caused the error.
* @returns A mock Error that represents not having been able to fetch the Resource at `url` due to a 404 Response.
* @since 1.1.0
*/
export function mockFetchError(
fetchedUrl: UrlString,
statusCode: number = 404
): FetchError {
const failedResponse = new Response(undefined, {
status: statusCode,
}) as Response & { ok: false };
return new FetchError(
`Fetching the Resource at [${fetchedUrl}] failed: [${failedResponse.status}] [${failedResponse.statusText}].`,
failedResponse
);
}