forked from DevExpress/testcafe-hammerhead
-
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.
[RequestHook]: add methods for manipulation with response headers (Cl…
…oses DevExpress#1657) (DevExpress#1790) * Splited header-transforms * ConfigureResponseEvent: `setHeader` & `removeHeader` * test/server/request-hook-test: Added ConfigureResponseEvent `removeHeader` & `setHeader` test * test/server/proxy-test: Added Response header modification test cafe * Renamed `header-transforms` files and exports * fixed test/server/proxy-test typo * request-pipeline: splitted `callResponseEventCallbackForProcessedRequest` in order to send `onResponse` on `ctx.res.end()` callback
- Loading branch information
1 parent
53a9635
commit 0c1ef2f
Showing
6 changed files
with
149 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
requestTransforms, | ||
forcedRequestTransforms, | ||
responseTransforms, | ||
forcedResponseTransforms | ||
} from './transforms'; | ||
|
||
// Transformation routine | ||
function transformHeaders (srcHeaders, ctx, transformList, forcedTransforms) { | ||
const destHeaders = {}; | ||
|
||
const applyTransform = function (headerName, headers, transforms) { | ||
const src = headers[headerName]; | ||
const transform = transforms[headerName]; | ||
const dest = transform ? transform(src, ctx) : src; | ||
|
||
if (dest !== void 0) | ||
destHeaders[headerName] = dest; | ||
}; | ||
|
||
Object.keys(srcHeaders).forEach(headerName => applyTransform(headerName, srcHeaders, transformList)); | ||
|
||
if (forcedTransforms) | ||
Object.keys(forcedTransforms).forEach(headerName => applyTransform(headerName, destHeaders, forcedTransforms)); | ||
|
||
return destHeaders; | ||
} | ||
|
||
// API | ||
export function forRequest (ctx) { | ||
return transformHeaders(ctx.req.headers, ctx, requestTransforms, forcedRequestTransforms); | ||
} | ||
|
||
export function forResponse (ctx) { | ||
return transformHeaders(ctx.destRes.headers, ctx, responseTransforms, forcedResponseTransforms); | ||
} | ||
|
||
export function transformHeadersCaseToRaw (headers, rawHeaders) { | ||
const processedHeaders = {}; | ||
const headersNames = Object.keys(headers); | ||
|
||
for (let i = 0; i < rawHeaders.length; i += 2) { | ||
const rawHeaderName = rawHeaders[i]; | ||
const headerName = rawHeaderName.toLowerCase(); | ||
const headerIndex = headersNames.indexOf(headerName); | ||
|
||
if (headerIndex > -1) { | ||
processedHeaders[rawHeaderName] = headers[headerName]; | ||
headersNames[headerIndex] = void 0; | ||
} | ||
} | ||
|
||
for (const headerName of headersNames) { | ||
if (headerName !== void 0) | ||
processedHeaders[headerName] = headers[headerName]; | ||
} | ||
|
||
return processedHeaders; | ||
} |
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 |
---|---|---|
@@ -1,6 +1,15 @@ | ||
export default class ConfigureResponseEvent { | ||
constructor (requestFilterRule, opts) { | ||
constructor (requestContext, requestFilterRule, opts) { | ||
this._requestContext = requestContext; | ||
this._requestFilterRule = requestFilterRule; | ||
this.opts = opts; | ||
} | ||
|
||
setHeader (name, value) { | ||
this._requestContext.destRes.headers[name.toLowerCase()] = value; | ||
} | ||
|
||
removeHeader (name) { | ||
delete this._requestContext.destRes.headers[name.toLowerCase()]; | ||
} | ||
} |
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