Skip to content

Commit

Permalink
test: fix other tests which resolve URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Aug 17, 2022
1 parent f883d78 commit 2befbd5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
20 changes: 14 additions & 6 deletions tests/ResponseWrapper-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ describe('ResponseWrapper', () => {
describe('apiDocumentationLink', () => {
it('should get absolute link', async () => {
// given
const xhrResponse = await responseBuilder().body(Bodies.someJsonLd).apiDocumentation().build()
const xhrResponse = await responseBuilder().body(Bodies.someJsonLd).apiDocumentation().build({
url: 'http://resources.example.com/',
})

// when
const res = new ResponseWrapper('http://resources.example.com/', xhrResponse, parsers)
Expand All @@ -28,7 +30,9 @@ describe('ResponseWrapper', () => {

it('should get relative link', async () => {
// given
const xhrResponse = await responseBuilder().body(Bodies.someJsonLd).apiDocumentation('doc/').build()
const xhrResponse = await responseBuilder().body(Bodies.someJsonLd).apiDocumentation('doc/').build({
url: 'http://api.example.com/resource/',
})

// when
const res = new ResponseWrapper('http://api.example.com/resource/', xhrResponse, parsers)
Expand Down Expand Up @@ -91,6 +95,7 @@ describe('ResponseWrapper', () => {
it('returns absolute URL resolved to request base', () => {
// given
const xhrResponse = {
url: 'http://localhost:9876/to-strip',
} as any
const wrapper = new ResponseWrapper('http://localhost:9876/to-strip', xhrResponse, parsers)

Expand Down Expand Up @@ -166,6 +171,7 @@ describe('ResponseWrapper', () => {
it('should apply effectiveUri as baseIRI parser option', async () => {
// given
const xhrResponse = {
url: 'http://example.com/res',
headers: new Headers({
'content-type': 'application/ld+json',
}),
Expand Down Expand Up @@ -272,7 +278,9 @@ describe('ResponseWrapper', () => {
// given
const redirectUri = 'http://example.com/canonical'

const xhrResponse = await responseBuilder().body(Bodies.someJsonLd).canonical(redirectUri).build()
const xhrResponse = await responseBuilder().body(Bodies.someJsonLd).canonical(redirectUri).build({
url: 'http://example.com/original',
})
const response = new ResponseWrapper('http://example.com/original', xhrResponse, parsers)

// when
Expand All @@ -287,7 +295,7 @@ describe('ResponseWrapper', () => {
// given
const xhrResponse = await responseBuilder()
.canonical('http://example.com/canonical')
.build()
.build({ url: 'http://example.com/original' })
const response = new ResponseWrapper('http://example.com/original', xhrResponse, parsers)

// when
Expand All @@ -304,7 +312,7 @@ describe('ResponseWrapper', () => {
const xhrResponse = await responseBuilder()
.header('Location', 'http://example.com/the-real-id')
.statusCode(201)
.build()
.build({ url: 'http://example.com/original' })
const response = new ResponseWrapper('http://example.com/original', xhrResponse, parsers)

// when
Expand All @@ -318,7 +326,7 @@ describe('ResponseWrapper', () => {
// given
const xhrResponse = await responseBuilder()
.header('Location', 'http://example.com/the-real-id')
.build()
.build({ url: 'http://example.com/original' })
const response = new ResponseWrapper('http://example.com/original', xhrResponse, parsers)

// when
Expand Down
13 changes: 7 additions & 6 deletions tests/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const parser = new Parser()
export function responseBuilder() {
let statusCode = 200
let responseBody: any
let responseUri
let responseUri: string
const headers = {
'Content-Type': MediaTypes.jsonLd,
} as any
Expand Down Expand Up @@ -67,15 +67,16 @@ export function responseBuilder() {
return this.link(docUri, 'http://www.w3.org/ns/hydra/core#apiDocumentation')
},

build(): Promise<Response> {
build({ url }: { url?: string } = {}): Promise<Response> {
let response

if (responseUri) {
response = Response.redirect(responseUri, 302)
} else {
response = new Response(responseBody, {
response = new Response(responseBody, <any>{
headers: new Headers(headers),
status: statusCode,
url,
})
}

Expand All @@ -85,22 +86,22 @@ export function responseBuilder() {
}
}

export function mockedResponse({ includeDocsLink = true, xhrBuilder }): (uri: string) => Promise<ResponseWrapper> {
export function mockedResponse({ includeDocsLink = true, xhrBuilder }: { includeDocsLink: boolean; xhrBuilder: ReturnType<typeof responseBuilder> }): (uri: string) => Promise<ResponseWrapper> {
xhrBuilder = xhrBuilder || responseBuilder()

return async (requestedUri: string) => {
const xhr = await xhrBuilder.build()

const response: Omit<ResponseWrapper, 'xhr'> = {
apiDocumentationLink: includeDocsLink ? 'http://api.example.com/doc/' : null,
mediaType: xhr.headers.get('Content-Type'),
mediaType: xhr.headers.get('Content-Type')!,
redirectUrl: null,
quadStream() {
if (!xhr.body) {
return null
}

return parsers.import(this.mediaType, xhr.body)
return parsers.import(this.mediaType, xhr.body as any)
},
requestedUri,
resourceUri: requestedUri,
Expand Down

0 comments on commit 2befbd5

Please sign in to comment.