deno.land / x / alosaur@v1.1.1 / e2e / auth.test.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
import { assert, assertEquals } from "../src/deps_test.ts";import { killServer, startServer } from "./test.utils.ts";const { test } = Deno;
/** * Test cases */test({ name: "[http] auth server should redirect to login for protected url", async fn(): Promise<void> { const process = await startServer("./examples/auth/app.ts"); const baseUrl = "http://localhost:8000";
try { const response = await fetch(baseUrl + "/protected");
await response.arrayBuffer();
assertEquals(response.status, 200); assertEquals(response.url, "http://localhost:8000/account/login"); } finally { killServer(process); } },});
test({ name: "[http] auth server, should auth and gets protected info", async fn(): Promise<void> { const process = await startServer("./examples/auth/app.ts"); try { const formdata = new URLSearchParams(); formdata.append("login", "admin"); formdata.append("password", "admin");
const authHeaders = new Headers(); authHeaders.append("Content-Type", "application/x-www-form-urlencoded");
const response = await fetch("http://localhost:8000/account/login-json", { method: "POST", headers: authHeaders, body: formdata, redirect: "error", });
await response.arrayBuffer();
/** * PART 2 of Auth request: */ console.log(response.headers.get("set-cookie")!);
const cookies = response.headers.get("set-cookie")!.replace( "sid=", "", ).split(", "); const sid = cookies[0].split(";")[0]; const sign = cookies[1].replace("sid-s=", "").split(";")[0];
assert(sid); assert(sign);
const headers = new Headers(); headers.set("Cookie", "sid=" + sid + "; sid-s=" + sign); headers.append("Content-Type", "text/plain");
const responseAuth = await fetch("http://localhost:8000/protected", { method: "GET", headers: headers, credentials: "include", });
assertEquals(responseAuth.status, 200); assertEquals(responseAuth.url, "http://localhost:8000/protected");
assertEquals( await responseAuth.text(), "Hi! this protected info. <br> <a href='/account/logout'>logout</a>", ); } finally { killServer(process); } },});
alosaur

Version Info

Tagged at
2 months ago