Skip to content

Commit

Permalink
feat(ui): Plugin / Script > rf.response.setHeader(name, value)
Browse files Browse the repository at this point in the history
  • Loading branch information
flawiddsouza committed Oct 21, 2024
1 parent d65741a commit 3a6a96b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/plugins/available-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
| `rf.response.getURL()` | Retrieves the URL from the response. |
| `rf.response.getHeader(name)` | Gets a specific header's value by name from the response. |
| `rf.response.getHeaders()` | Gets all the headers from the response. |
| `rf.response.setHeader(name, value)` | Sets a specific header's value for the response. |
| `rf.response.getBody()` | Retrieves the response body as an ArrayBuffer. |
| `rf.response.setBody(body)` | Sets the response body with the provided ArrayBuffer. |
| `rf.response.getBodyText()` | Returns the response body as text. |
Expand Down
5 changes: 5 additions & 0 deletions packages/ui/src/components/SnippetDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ export default defineComponent ({
label: 'Gets all the headers from the response',
value: 'rf.response.getHeaders()'
},
{
type: 'option',
label: 'Sets a specific header\'s value for the response',
value: 'rf.response.setHeader(name, value)'
},
{
type: 'option',
label: 'Retrieves the response body as an ArrayBuffer',
Expand Down
6 changes: 6 additions & 0 deletions packages/ui/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ export default {
type: 'function',
info: 'Gets all the headers from the response'
},
{
label: 'rf.response.setHeader(name, value)',
type: 'function',
info: 'Sets a specific header value for the response',
apply: snippet('rf.response.setHeader(${name}, ${value})')
},
{
label: 'rf.response.getBody()',
type: 'function',
Expand Down
8 changes: 8 additions & 0 deletions packages/ui/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ export function createResponseContextForPlugin(response: RequestFinalResponse, e
const header = headers.find((header: RequestInitialResponseHeader) => header[0].toLowerCase() == headerName.toLowerCase())
return header ? header[1] : undefined
},
setHeader(headerName: string, value: string) {
const headerIndex = headers.findIndex((header: RequestInitialResponseHeader) => header[0].toLowerCase() === headerName.toLowerCase())
if (headerIndex >= 0) {
headers[headerIndex][1] = value
} else {
headers.push([headerName, value])
}
},
getStatusCode() {
return statusCode
},
Expand Down

0 comments on commit 3a6a96b

Please sign in to comment.