-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added utility to handle various body formats (#14)
* added utility to handle various body formats * fixed typos
- Loading branch information
1 parent
58d77ac
commit 2bd431d
Showing
5 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export default (body) => { | ||
// Handle empty case | ||
if (!body) return null | ||
// Handle FormData | ||
if (body instanceof FormData) return body | ||
try { | ||
// Handle already stringified JSON | ||
JSON.parse(body) | ||
return body | ||
} catch (err) { | ||
// Handle plain object | ||
return JSON.stringify(body) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import 'babel-polyfill' | ||
import FormData from 'form-data' | ||
import { expect } from 'chai' | ||
|
||
global.expect = expect | ||
global.FormData = FormData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import processBody from '../../src/utilities/body' | ||
|
||
describe('processBody', () => { | ||
it('returns null when no value is passed', () => { | ||
const body = processBody() | ||
expect(body).to.equal(null) | ||
}) | ||
|
||
it('returns null when empty string is passed', () => { | ||
const body = processBody('') | ||
expect(body).to.equal(null) | ||
}) | ||
|
||
it('returns input when FormData is passed', () => { | ||
const fd = new FormData() | ||
const body = processBody(fd) | ||
expect(body).to.equal(fd) | ||
}) | ||
|
||
it('returns input when stringified data is passed', () => { | ||
const data = JSON.stringify({value: 'test'}) | ||
const body = processBody(data) | ||
expect(body).to.equal(data) | ||
}) | ||
|
||
it('returns stringified data when object is passed', () => { | ||
const rawData = {value: 'test'} | ||
const data = JSON.stringify(rawData) | ||
const body = processBody(rawData) | ||
expect(body).to.equal(data) | ||
}) | ||
}) |