Skip to content

Commit ae3f664

Browse files
committed
tests for exercise 2 done
1 parent 51ea577 commit ae3f664

File tree

4 files changed

+100
-8
lines changed

4 files changed

+100
-8
lines changed

exercises/02.init/01.problem.authenticate/test/index.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { test, expect, inject } from 'vitest'
2-
import { z } from 'zod'
32

43
const mcpServerPort = inject('mcpServerPort')
54
const mcpServerUrl = `http://localhost:${mcpServerPort}`

exercises/02.init/01.solution.authenticate/test/index.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { test, expect, inject } from 'vitest'
2-
import { z } from 'zod'
32

43
const mcpServerPort = inject('mcpServerPort')
54
const mcpServerUrl = `http://localhost:${mcpServerPort}`
Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,56 @@
11
import { test, expect, inject } from 'vitest'
2-
import { z } from 'zod'
32

43
const mcpServerPort = inject('mcpServerPort')
54
const mcpServerUrl = `http://localhost:${mcpServerPort}`
65

7-
test(`TODO: update this test title to describe the important thing we're working on in this exercise step`, async () => {
8-
// TODO: implement this test
6+
test(`WWW-Authenticate header includes auth params when authorization is missing`, async () => {
7+
const response = await fetch(`${mcpServerUrl}/mcp`)
8+
9+
expect(
10+
response.status,
11+
'🚨 Request without Authorization header should return 401 status',
12+
).toBe(401)
13+
14+
const wwwAuthenticateHeader = response.headers.get('WWW-Authenticate')
15+
expect(
16+
wwwAuthenticateHeader,
17+
'🚨 Response should include WWW-Authenticate header',
18+
).toBeTruthy()
19+
20+
expect(
21+
wwwAuthenticateHeader?.includes('Bearer'),
22+
'🚨 WWW-Authenticate header should include Bearer scheme',
23+
).toBe(true)
24+
25+
expect(
26+
wwwAuthenticateHeader?.includes('realm="EpicMe"'),
27+
'🚨 WWW-Authenticate header should include realm="EpicMe"',
28+
).toBe(true)
29+
30+
expect(
31+
wwwAuthenticateHeader?.includes('resource_metadata='),
32+
'🚨 WWW-Authenticate header should include resource_metadata parameter',
33+
).toBe(true)
34+
35+
// Extract the resource_metadata URL from the header
36+
const resourceMetadataMatch = wwwAuthenticateHeader?.match(
37+
/resource_metadata=([^,]+)/,
38+
)
39+
expect(
40+
resourceMetadataMatch,
41+
'🚨 Should be able to extract resource_metadata URL from WWW-Authenticate header',
42+
).toBeTruthy()
43+
44+
const resourceMetadataUrl = resourceMetadataMatch?.[1]
45+
expect(
46+
resourceMetadataUrl,
47+
'🚨 resource_metadata URL should not be empty',
48+
).toBeTruthy()
49+
50+
// Verify the resource_metadata URL points to the correct endpoint
51+
const expectedResourceMetadataUrl = `${mcpServerUrl}/.well-known/oauth-protected-resource/mcp`
52+
expect(
53+
resourceMetadataUrl,
54+
`🚨 resource_metadata should point to ${expectedResourceMetadataUrl}`,
55+
).toBe(expectedResourceMetadataUrl)
956
})
Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,56 @@
11
import { test, expect, inject } from 'vitest'
2-
import { z } from 'zod'
32

43
const mcpServerPort = inject('mcpServerPort')
54
const mcpServerUrl = `http://localhost:${mcpServerPort}`
65

7-
test(`TODO: update this test title to describe the important thing we're working on in this exercise step`, async () => {
8-
// TODO: implement this test
6+
test(`WWW-Authenticate header includes auth params when authorization is missing`, async () => {
7+
const response = await fetch(`${mcpServerUrl}/mcp`)
8+
9+
expect(
10+
response.status,
11+
'🚨 Request without Authorization header should return 401 status',
12+
).toBe(401)
13+
14+
const wwwAuthenticateHeader = response.headers.get('WWW-Authenticate')
15+
expect(
16+
wwwAuthenticateHeader,
17+
'🚨 Response should include WWW-Authenticate header',
18+
).toBeTruthy()
19+
20+
expect(
21+
wwwAuthenticateHeader?.includes('Bearer'),
22+
'🚨 WWW-Authenticate header should include Bearer scheme',
23+
).toBe(true)
24+
25+
expect(
26+
wwwAuthenticateHeader?.includes('realm="EpicMe"'),
27+
'🚨 WWW-Authenticate header should include realm="EpicMe"',
28+
).toBe(true)
29+
30+
expect(
31+
wwwAuthenticateHeader?.includes('resource_metadata='),
32+
'🚨 WWW-Authenticate header should include resource_metadata parameter',
33+
).toBe(true)
34+
35+
// Extract the resource_metadata URL from the header
36+
const resourceMetadataMatch = wwwAuthenticateHeader?.match(
37+
/resource_metadata=([^,]+)/,
38+
)
39+
expect(
40+
resourceMetadataMatch,
41+
'🚨 Should be able to extract resource_metadata URL from WWW-Authenticate header',
42+
).toBeTruthy()
43+
44+
const resourceMetadataUrl = resourceMetadataMatch?.[1]
45+
expect(
46+
resourceMetadataUrl,
47+
'🚨 resource_metadata URL should not be empty',
48+
).toBeTruthy()
49+
50+
// Verify the resource_metadata URL points to the correct endpoint
51+
const expectedResourceMetadataUrl = `${mcpServerUrl}/.well-known/oauth-protected-resource/mcp`
52+
expect(
53+
resourceMetadataUrl,
54+
`🚨 resource_metadata should point to ${expectedResourceMetadataUrl}`,
55+
).toBe(expectedResourceMetadataUrl)
956
})

0 commit comments

Comments
 (0)