Skip to content

Commit

Permalink
fix: validateBody for API Explorer (#846)
Browse files Browse the repository at this point in the history
* fix: validateBody for already-parsed JSON

* removed stale code comment
  • Loading branch information
jkaster authored Oct 6, 2021
1 parent 4ece1b3 commit d1f203c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/run-it/src/components/RequestForm/formUtils.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ describe('Complex Item', () => {
describe('validateBody', () => {
test.each`
value | error
${{
model: 'thelook',
view: 'users',
fields: ['users.id', 'users.first_name'],
}} | ${''}
${'na.-_me=Vapor&age=3&luckyNumbers[]=5&luckyNumbers[]=7'} | ${''}
${'name=Vapor&age=3&luckyNumbers[]=5&luckyNumbers[]7'} | ${'luckyNumbers[]7'}
${'{'} | ${'Unexpected end of JSON input'}
Expand Down
4 changes: 2 additions & 2 deletions packages/run-it/src/components/RequestForm/formUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@ export const validateEncodedValues = (body: string) => {
*
* @param body string to validate
*/
export const validateBody = (body: string) => {
export const validateBody = (body: string | Record<string, any>) => {
let result = ''
if (body) {
if (body && typeof body === 'string') {
if (/^[[{}"]/.test(body)) {
// most likely JSON
try {
Expand Down
40 changes: 40 additions & 0 deletions packages/sdk-node/test/methods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,46 @@ describe('LookerNodeSDK', () => {
},
testTimeout
)

it(
'parses a query with no results',
async () => {
const sdk = new LookerSDK(session)
const query = await sdk.ok(
sdk.create_query({
model: 'system__activity',
view: 'dashboard',
limit: '2',
fields: ['dashboard.id', 'dashboard.title'],
filters: { 'dashboard.id': '-1' },
})
)
expect(query).toBeDefined()
expect(query.id).toBeDefined()
for (const format of ['csv', 'json', 'json_detail', 'txt', 'md']) {
let failed = ''
try {
const live = await sdk.ok(
sdk.run_query({ query_id: query.id!, result_format: format })
)
const cached = await sdk.ok(
sdk.run_query({
query_id: query.id!,
result_format: format,
cache: true,
})
)
expect(live).not.toEqual('{}')
expect(cached).not.toEqual('{}')
} catch (e: any) {
failed = e.message
}
expect(failed).toEqual('')
}
await sdk.authSession.logout()
},
testTimeout
)
})

describe('Dashboard endpoints', () => {
Expand Down

0 comments on commit d1f203c

Please sign in to comment.