Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions probitas/03-client-http.probitas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
import { client, expect, scenario, Skip } from "jsr:@probitas/probitas@^0";

const BASE_URL = "http://localhost:8080";
const BASE_URL = Deno.env.get("ECHO_HTTP_URL") ?? "http://localhost:8080";

export default scenario("HTTP Client Example", {
tags: ["integration", "http"],
Expand Down Expand Up @@ -128,7 +128,7 @@ export default scenario("HTTP Client Example", {
.step("GET /basic-auth - valid credentials", async (ctx) => {
const { http } = ctx.resources;
const credentials = btoa("testuser:testpass");
const res = await http.get("/basic-auth/testuser/testpass", {
const res = await http.get("/basic-auth", {
headers: { Authorization: `Basic ${credentials}` },
});

Expand All @@ -139,26 +139,28 @@ export default scenario("HTTP Client Example", {
.step("GET /basic-auth - invalid credentials", async (ctx) => {
const { http } = ctx.resources;
const credentials = btoa("wronguser:wrongpass");
const res = await http.get("/basic-auth/testuser/testpass", {
const res = await http.get("/basic-auth", {
headers: { Authorization: `Basic ${credentials}` },
throwOnError: false,
});

expect(res).not.toBeOk().toHaveStatus(401);
})
.step("GET /bearer - valid token", async (ctx) => {
.step("GET /bearer-auth - valid token", async (ctx) => {
const { http } = ctx.resources;
const res = await http.get("/bearer", {
headers: { Authorization: "Bearer my-secret-token" },
// Token is SHA1(testuser:testpass)
const token = "1eac13f1578ef493b9ed5617a5f4a31b271eb667";
const res = await http.get("/bearer-auth", {
headers: { Authorization: `Bearer ${token}` },
});

expect(res)
.toBeOk()
.toHaveJsonMatching({ authenticated: true, token: "my-secret-token" });
.toHaveJsonMatching({ authenticated: true, token });
})
.step("GET /bearer - missing token", async (ctx) => {
.step("GET /bearer-auth - missing token", async (ctx) => {
const { http } = ctx.resources;
const res = await http.get("/bearer", { throwOnError: false });
const res = await http.get("/bearer-auth", { throwOnError: false });

expect(res).not.toBeOk().toHaveStatus(401);
})
Expand Down
55 changes: 29 additions & 26 deletions probitas/16-client-http-oidc.probitas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
*/
import { client, expect, scenario, Skip } from "jsr:@probitas/probitas@^0";

const BASE_URL = "http://localhost:8080";
const BASE_URL = Deno.env.get("ECHO_HTTP_URL") ?? "http://localhost:8080";
const TEST_USER = "testuser";
const TEST_PASS = "testpass";
const ISSUER = `${BASE_URL}/oidc/${TEST_USER}/${TEST_PASS}`;
const ISSUER = BASE_URL;

export default scenario("HTTP Client OIDC Example", {
tags: ["integration", "http", "oidc"],
Expand All @@ -38,16 +38,16 @@ export default scenario("HTTP Client OIDC Example", {
async (ctx) => {
const { "http-client": http } = ctx.resources;
const res = await http.get(
`/oidc/${TEST_USER}/${TEST_PASS}/.well-known/openid-configuration`,
`/.well-known/openid-configuration`,
);

expect(res)
.toBeOk()
.toHaveStatus(200)
.toHaveJsonMatching({
issuer: ISSUER,
authorization_endpoint: `${ISSUER}/authorize`,
token_endpoint: `${ISSUER}/token`,
authorization_endpoint: `${ISSUER}/oauth2/authorize`,
token_endpoint: `${ISSUER}/oauth2/token`,
});
},
)
Expand Down Expand Up @@ -83,16 +83,16 @@ export default scenario("HTTP Client OIDC Example", {
},
)
.step(
"GET /bearer - authenticated request with auto header",
"GET /oauth2/userinfo - authenticated request with auto header",
async (ctx) => {
const { "oidc-http-with-discovery": http } = ctx.resources;
const res = await http.get("/bearer");
const res = await http.get("/oauth2/userinfo");

expect(res)
.toBeOk()
.toHaveStatus(200)
.toHaveJsonMatching({
authenticated: true,
sub: TEST_USER,
});
},
)
Expand All @@ -102,8 +102,8 @@ export default scenario("HTTP Client OIDC Example", {
await client.http.oidc.createOidcHttpClient({
url: BASE_URL,
oidc: {
authUrl: `/oidc/${TEST_USER}/${TEST_PASS}/authorize`,
tokenUrl: `/oidc/${TEST_USER}/${TEST_PASS}/token`,
authUrl: `/oauth2/authorize`,
tokenUrl: `/oauth2/token`,
clientId: "test-client-manual",
},
}),
Expand All @@ -124,21 +124,24 @@ export default scenario("HTTP Client OIDC Example", {

return result;
})
.step("GET /bearer - authenticated with manual config", async (ctx) => {
const { "oidc-http-manual": http } = ctx.resources;
const res = await http.get("/bearer");
.step(
"GET /oauth2/userinfo - authenticated with manual config",
async (ctx) => {
const { "oidc-http-manual": http } = ctx.resources;
const res = await http.get("/oauth2/userinfo");

expect(res)
.toBeOk()
.toHaveJsonMatching({
authenticated: true,
});
})
expect(res)
.toBeOk()
.toHaveJsonMatching({
sub: TEST_USER,
});
},
)
.step("Login failure - invalid credentials", async () => {
const http = await client.http.oidc.createOidcHttpClient({
url: BASE_URL,
oidc: {
issuer: `${BASE_URL}/oidc/${TEST_USER}/${TEST_PASS}`,
issuer: BASE_URL,
clientId: "test-client",
},
});
Expand All @@ -160,16 +163,16 @@ export default scenario("HTTP Client OIDC Example", {
const { "oidc-http-with-discovery": http } = ctx.resources;

// First request
const res1 = await http.get("/bearer");
expect(res1).toBeOk().toHaveJsonMatching({ authenticated: true });
const res1 = await http.get("/oauth2/userinfo");
expect(res1).toBeOk().toHaveJsonMatching({ sub: TEST_USER });

// Second request - token should still be valid
const res2 = await http.get("/bearer");
expect(res2).toBeOk().toHaveJsonMatching({ authenticated: true });
const res2 = await http.get("/oauth2/userinfo");
expect(res2).toBeOk().toHaveJsonMatching({ sub: TEST_USER });

// Third request with query parameters
const res3 = await http.get("/bearer?foo=bar");
expect(res3).toBeOk().toHaveJsonMatching({ authenticated: true });
const res3 = await http.get("/oauth2/userinfo?foo=bar");
expect(res3).toBeOk().toHaveJsonMatching({ sub: TEST_USER });
})
.step("POST request with authentication", async (ctx) => {
const { "oidc-http-with-discovery": http } = ctx.resources;
Expand Down