Skip to content

Commit 0e58924

Browse files
authored
Merge pull request #104 from filiphric/100-xml-request-and-response-show-in-single-line-instead-of-showing-proper-xml
feat: add support for missing or unknown content-type
2 parents 36d4064 + 33d3c67 commit 0e58924

File tree

13 files changed

+90
-20
lines changed

13 files changed

+90
-20
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
## [2.10.0](https://github.com/filiphric/cypress-plugin-api/compare/v2.9.1...v2.10.0) (2023-01-15)
6+
7+
8+
### Features
9+
10+
* add support for missing or unknown content-type ([d283156](https://github.com/filiphric/cypress-plugin-api/commits/d28315664d1a800ae8a1a0543418c41b10f90f6f))
11+
512
### [2.9.1](https://github.com/filiphric/cypress-plugin-api/compare/v2.9.0...v2.9.1) (2023-01-11)
613

714

cypress/e2e/formats.cy.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,31 @@ describe('response formats', () => {
22

33
it('works with xml', () => {
44

5-
cy.api({
6-
url: '/xml'
7-
})
5+
cy.api('/xml')
6+
7+
// xml tag is formatted
8+
cy.contains('xml')
9+
.should('have.css', 'color', 'rgb(100, 112, 243)')
810

911
});
1012

1113
it('works with html', () => {
1214

13-
cy.api({
14-
url: '/html'
15-
})
15+
cy.api('/html')
16+
17+
// html tag is formatted
18+
cy.contains('html')
19+
.should('have.css', 'color', 'rgb(255, 87, 112)')
1620

1721
});
1822

1923
it('works with json', () => {
2024

21-
cy.api({
22-
url: '/json'
23-
})
25+
cy.api('/json')
26+
27+
// numbers in json are formatted
28+
cy.contains('1234')
29+
.should('have.css', 'color', 'rgb(31, 169, 113)')
2430

2531
});
2632

@@ -34,4 +40,14 @@ describe('response formats', () => {
3440

3541
});
3642

43+
it('works with undefined format', () => {
44+
45+
cy.api('/undefined')
46+
47+
// xml tag is formatted
48+
cy.contains('xml')
49+
.should('have.css', 'color', 'rgb(100, 112, 243)')
50+
51+
});
52+
3753
});

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "Filip Hric (https://filiphric.com/)",
33
"license": "ISC",
44
"name": "cypress-plugin-api",
5-
"version": "2.9.1",
5+
"version": "2.10.0",
66
"keywords": [
77
"cypress",
88
"api",

server/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ app.get('/xml', (req, res) => {
8484
res.send(answerXML)
8585
})
8686

87+
app.get('/undefined', (req, res) => {
88+
const answerXML = "<xml>XML</xml>"
89+
res.send(answerXML)
90+
})
91+
8792
app.get('/text', (req, res) => {
8893
const answerText = "Hey there 👋"
8994
res.send(answerText)

src/modules/handleResponse.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ApiRequestOptions, ApiResponseBody, RequestProps } from 'src/types';
66
import { transform } from "@modules/transform";
77
import { getState } from '@utils/getState';
88
import { App } from 'vue';
9+
import { getFormat } from '@utils/getFormat';
910

1011
export const handleResponse = (res: ApiResponseBody, options: ApiRequestOptions, props: RequestProps[], index: number, app: App<Element>) => {
1112

@@ -37,7 +38,9 @@ export const handleResponse = (res: ApiResponseBody, options: ApiRequestOptions,
3738
'text/html': 'html',
3839
'text/plain': 'plaintext',
3940
} as const
40-
const language = formats[contentType as keyof typeof formats]
41+
const definedFormat = formats[contentType as keyof typeof formats]
42+
// if format is in the "formats" object use that, else try to determine by the function
43+
const language = definedFormat || getFormat(body)
4144
// format response
4245
props[index].responseBody.formatted = transform(body, language)
4346
props[index].responseBody.body = bodyRaw

src/modules/transform.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as Prism from 'prismjs';
22
import 'prismjs/components/prism-json';
3-
import { isStringValidJson } from '../utils/isStringValidJson';
3+
import { isValidJson } from '../utils/isValidJson';
44

5-
export const transform = (body: any, language: 'json' | 'html' | 'xml' | 'plaintext' = 'json') => {
5+
export const transform = (body: any, language: 'json' | 'html' | 'xml' | 'blob' | 'plaintext' = 'json') => {
66
const content = language === 'json' ? JSON.stringify(body, null, 2) : body
77
if (body) {
88
const formatted = Prism.highlight(content, Prism.languages[language], language)
@@ -13,7 +13,7 @@ export const transform = (body: any, language: 'json' | 'html' | 'xml' | 'plaint
1313

1414

1515
// add folding to every json object and array
16-
if (isStringValidJson(content)) {
16+
if (isValidJson(content)) {
1717
code = code
1818
.replaceAll('<span class="token punctuation">{</span>', '<details class="contents" open><summary class="inline-block brace"><span class="token punctuation">{</span></summary>')
1919
.replaceAll('<span class="token punctuation">[</span>', '<details class="contents" open><summary class="inline-block bracket"><span class="token punctuation">[</span></summary>')

src/utils/calculateSize.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isStringValidJson } from "./isStringValidJson"
1+
import { isValidJson } from "./isValidJson"
22

33
export const calculateSize = (value: object) => {
44

@@ -10,7 +10,7 @@ export const calculateSize = (value: object) => {
1010
// remove all carriage return symbols
1111
const cleanString = stringified.replace(/\r\n/g, '\n')
1212
// replace white spaces if value is a JSON
13-
const finalString = isStringValidJson(cleanString) ? cleanString.replace(/\s/g, '') : cleanString
13+
const finalString = isValidJson(cleanString) ? cleanString.replace(/\s/g, '') : cleanString
1414
const byteCount = new Blob([finalString]).size
1515

1616
return byteCount

src/utils/getFormat.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { isValidJson } from "@utils/isValidJson";
2+
import { isValidXml } from "@utils/isValidXml";
3+
import { isValidHtml } from "@utils/isValidHtml";
4+
import { isValidBlob } from "@utils/isValidBlob";
5+
6+
export function getFormat(str: string) {
7+
if (isValidJson(str)) {
8+
return "json";
9+
} else if (isValidXml(str)) {
10+
return "xml";
11+
} else if (isValidHtml(str)) {
12+
return "html";
13+
} else if (isValidBlob(str)) {
14+
return "blob";
15+
} else {
16+
return "plaintext";
17+
}
18+
}

src/utils/isValidBlob.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function isValidBlob(str: any) {
2+
return str instanceof Blob;
3+
}

0 commit comments

Comments
 (0)