-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_context.ts
More file actions
94 lines (85 loc) · 2.83 KB
/
test_context.ts
File metadata and controls
94 lines (85 loc) · 2.83 KB
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
import { OAuth2Request, OAuth2Response } from "./context.ts";
import { Client } from "./models/client.ts";
import { Scope } from "./models/scope.ts";
import { User } from "./models/user.ts";
export function fakeTokenRequest(
body?: string | URLSearchParams | string[][] | Record<string, string>,
): OAuth2Request<Client, User, Scope> {
const params: URLSearchParams | undefined = typeof body === "undefined"
? undefined
: new URLSearchParams(body);
const request: OAuth2Request<Client, User, Scope> = {
url: new URL("https://example.com/token"),
headers: new Headers(),
method: "POST",
get body() {
return Promise.resolve(params ?? new URLSearchParams());
},
};
request.headers.set("authorization", `basic ${btoa("1:")}`);
if (params) {
request.headers.set("Content-Type", "application/x-www-form-urlencoded");
}
return request;
}
export function fakeResourceRequest(
bearerToken: string,
body?: string | URLSearchParams | string[][] | Record<string, string>,
): OAuth2Request<Client, User, Scope> {
const params: URLSearchParams | undefined = typeof body === "undefined"
? undefined
: new URLSearchParams(body);
const request: OAuth2Request<Client, User, Scope> = {
url: new URL("https://example.com/resource/1"),
headers: new Headers(),
method: params ? "POST" : "GET",
get body() {
return Promise.resolve(params ?? new URLSearchParams());
},
};
if (bearerToken) {
request.headers.set("authorization", `bearer ${bearerToken}`);
}
if (params) {
request.headers.set("Content-Type", "application/x-www-form-urlencoded");
}
return request;
}
export function fakeAuthorizeRequest(
body?: string | URLSearchParams | string[][] | Record<string, string>,
): OAuth2Request<Client, User, Scope> {
const bodyParams: URLSearchParams | undefined = typeof body === "undefined"
? undefined
: new URLSearchParams(body);
const url = new URL(`https://example.com/authorize`);
const { searchParams } = url;
searchParams.set("response_type", "code");
searchParams.set("client_id", "1");
searchParams.set("redirect_uri", "https://client.example.com/cb");
searchParams.set("state", "xyz");
searchParams.set("scope", "read write");
const request: OAuth2Request<Client, User, Scope> = {
url,
headers: new Headers(),
method: bodyParams ? "POST" : "GET",
get body() {
return Promise.resolve(bodyParams ?? new URLSearchParams());
},
};
if (bodyParams) {
request.headers.set("Content-Type", "application/x-www-form-urlencoded");
}
return request;
}
class FakeResponse implements OAuth2Response {
headers: Headers;
constructor() {
this.headers = new Headers();
}
async redirect(): Promise<void> {
return await Promise.resolve();
}
}
export function fakeResponse(): OAuth2Response {
return new FakeResponse();
}