-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
220 additions
and
9 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,43 @@ | ||
import { Js2Xml } from 'js2xml'; | ||
import { response } from '../src'; | ||
|
||
console.log = jest.fn(); | ||
console.error = jest.fn(); | ||
|
||
test('is should response without body', () => { | ||
const ctx: any = { | ||
headers: { | ||
accept: 'application/json', | ||
}, | ||
}; | ||
const body = { foo: 'bar' }; | ||
response(ctx, 200); | ||
expect(ctx.status).toBe(200); | ||
}); | ||
|
||
test('is should response json', () => { | ||
const ctx: any = { | ||
headers: { | ||
accept: 'application/json', | ||
}, | ||
}; | ||
const body = { foo: 'bar' }; | ||
response(ctx, 200, body); | ||
expect(ctx.body).toBe(body); | ||
expect(ctx.status).toBe(200); | ||
}); | ||
|
||
test('is should response xml', () => { | ||
const ctx: any = { | ||
headers: { | ||
accept: 'application/xml', | ||
}, | ||
}; | ||
const body = { foo: 'bar' }; | ||
const json = JSON.stringify(body); | ||
const obj = JSON.parse(json); | ||
const xml: any = new Js2Xml('xml', obj); | ||
response(ctx, 200, body); | ||
expect(ctx.body).toBe(xml.toString()); | ||
expect(ctx.status).toBe(200); | ||
}); |
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
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,25 @@ | ||
import { Js2Xml } from 'js2xml'; | ||
import { IContext } from '@ycs/interfaces'; | ||
|
||
/** | ||
* Generating output | ||
* @param ctx {IContext} Ycs context | ||
* @param status {number} status | ||
* @param body {body} body | ||
*/ | ||
export function response(ctx: IContext, status: number, body?: any) { | ||
if (!body) { | ||
ctx.type = 'text/plain'; | ||
} | ||
if ('application/xml' === ctx.headers.accept) { | ||
const json = JSON.stringify(body); | ||
const obj = JSON.parse(json); | ||
const xml: any = new Js2Xml('xml', obj); | ||
ctx.status = status; | ||
ctx.type = `${ctx.headers.accept}; charset=utf-8`; | ||
ctx.body = xml.toString(); | ||
} else { | ||
ctx.status = status; | ||
ctx.body = 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
Oops, something went wrong.