Skip to content

Commit

Permalink
feat: sort query string
Browse files Browse the repository at this point in the history
so the match does not depend on the order
  • Loading branch information
coderbyheart committed Apr 5, 2024
1 parent 0dd96c6 commit 5b41165
Show file tree
Hide file tree
Showing 9 changed files with 477 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test-and-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ jobs:
- name: Check if source code is properly formatted
run: npx prettier -c ./

- run: npm test

- name: Compile
run: npm run prepublishOnly

Expand Down
4 changes: 3 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
npx lint-staged
npx lint-staged
npm test
npx tsc
11 changes: 6 additions & 5 deletions cdk/resources/http-api-mock-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { URLSearchParams } from 'url'
import { logger } from '@hello.nrfcloud.com/lambda-helpers/logger'
import { checkMatchingQueryParams } from './checkMatchingQueryParams.js'
import { splitMockResponse } from './splitMockResponse.js'
import { sortQueryString } from '../../src/sortQueryString.js'

const db = new DynamoDBClient({})
const log = logger('httpApiMock')
Expand All @@ -29,10 +30,10 @@ export const handler = async (
event.queryStringParameters as Record<string, string>,
)
: undefined
const resource = event.path.replace(/^\//, '')
const pathWithQuery = `${resource}${
query !== undefined ? `?${query.toString()}` : ''
}`
const path = event.path.replace(/^\//, '')
const pathWithQuery = sortQueryString(
`${path}${query !== undefined ? `?${query.toString()}` : ''}`,
)

await db.send(
new PutItemCommand({
Expand All @@ -53,7 +54,7 @@ export const handler = async (
path: {
S: pathWithQuery,
},
resource: { S: resource },
resource: { S: path },
query:
query === undefined
? { NULL: true }
Expand Down
17 changes: 17 additions & 0 deletions cdk/resources/splitMockResponse.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { splitMockResponse } from './splitMockResponse.js'
import { describe, it } from 'node:test'
import assert from 'node:assert'
void describe('split mock response', () => {
void it('should parse headers and body', () =>
assert.deepEqual(
splitMockResponse(`Content-Type: application/octet-stream
(binary A-GNSS data) other types`),
{
headers: {
'Content-Type': 'application/octet-stream',
},
body: '(binary A-GNSS data) other types',
},
))
})
Loading

0 comments on commit 5b41165

Please sign in to comment.