Skip to content

Commit

Permalink
Fix more tests
Browse files Browse the repository at this point in the history
Normalize request differently to be more compatible
  • Loading branch information
dac09 committed Dec 22, 2023
1 parent 82badfd commit 3f0a32b
Show file tree
Hide file tree
Showing 6 changed files with 3,301 additions and 15 deletions.
30 changes: 30 additions & 0 deletions packages/api/src/__tests__/normalizeRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,34 @@ describe('Fetch API Request', () => {

expect(partial.headers.get('content-type')).toEqual('application/json')
})

it('Handles an empty body', async () => {
const headers = {
'content-type': 'application/json',
'x-custom-header': 'bazinga',
}

const fetchEvent = new Request(
'http://localhost:9210/graphql?whatsup=doc&its=bugs',
{
method: 'PUT',
headers,
body: '',
}
)

const partial = await normalizeRequest(fetchEvent)

expect(partial).toMatchObject({
method: 'PUT',
query: {
whatsup: 'doc',
its: 'bugs',
},
jsonBody: undefined,
})

expect(partial.headers.get('content-type')).toEqual(headers['content-type'])
expect(partial.headers.get('x-custom-header')).toEqual('bazinga')
})
})
23 changes: 22 additions & 1 deletion packages/api/src/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ export const parseLambdaEventBody = (event: APIGatewayProxyEvent) => {
}
}

/**
* Extracts and parses body payload from Fetch Request
* with check for empty body (and base64 later)
*/
export const parseFetchEventBody = async (event: Request) => {
if (!event.body) {
return
}

// @TODO: We need to understand what happens on Vercel
// if (event.isBase64Encoded) {
// return JSON.parse(Buffer.from(event.body, 'base64').toString('utf-8'))
// } else {
// return JSON.parse(event.body)
// }

const body = await event.text()

return body ? JSON.parse(body) : undefined
}

export const isFetchApiRequest = (event: any): event is Request => {
return event instanceof Request || event instanceof PonyFillRequest
}
Expand Down Expand Up @@ -56,7 +77,7 @@ export async function normalizeRequest(
headers: event.headers,
method: event.method,
query: getQueryStringParams(event.url),
jsonBody: await event.json(),
jsonBody: await parseFetchEventBody(event),
}
}

Expand Down
13 changes: 7 additions & 6 deletions packages/auth-providers/dbAuth/api/src/DbAuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ export class DbAuthHandler<
'set-cookie': [
`${cookieName(this.options.cookie?.name)}=`,
...this._cookieAttributes({ expires: 'now' }),
`auth-provider=`,
...this._cookieAttributes({ expires: 'now' }),
// `auth-provider=`,
// ...this._cookieAttributes({ expires: 'now' }),
].join(';'),
}
}
Expand Down Expand Up @@ -479,7 +479,7 @@ export class DbAuthHandler<
}

try {
const method = this._getAuthMethod()
const method = await this._getAuthMethod()

// get the auth method the incoming request is trying to call
if (!DbAuthHandler.METHODS.includes(method)) {
Expand Down Expand Up @@ -1208,8 +1208,8 @@ export class DbAuthHandler<
const cookie = [
`${cookieName(this.options.cookie?.name)}=${encrypted}`,
...this._cookieAttributes({ expires: this.sessionExpiresDate }),
'auth-provider=dbAuth',
...this._cookieAttributes({ expires: this.sessionExpiresDate }), // TODO need this to be not http-only
// 'auth-provider=dbAuth',
// ...this._cookieAttributes({ expires: this.sessionExpiresDate }), // TODO need this to be not http-only
].join(';')

return { 'set-cookie': cookie }
Expand Down Expand Up @@ -1446,7 +1446,8 @@ export class DbAuthHandler<
}

// figure out which auth method we're trying to call
_getAuthMethod() {
async _getAuthMethod() {
await this.init()
// try getting it from the query string, /.redwood/functions/auth?method=[methodName]
let methodName = this.normalizedRequest.query.method as AuthMethodNames

Expand Down
Loading

0 comments on commit 3f0a32b

Please sign in to comment.