From b1092d156762bd94061b44987ff838be2e3365ee Mon Sep 17 00:00:00 2001 From: Daniel Roop Date: Fri, 18 May 2018 21:39:49 -0400 Subject: [PATCH] Created helper to return all http headers for node middleware --- middleware/middleware.js | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/middleware/middleware.js b/middleware/middleware.js index 2548c7b..475e4fe 100755 --- a/middleware/middleware.js +++ b/middleware/middleware.js @@ -253,6 +253,21 @@ function setHttpStatus(payload, newStatus) { return setHttpPath(payload, newStatus); } +function httpHeaders(payload) { + var httpHeaderString = payload.slice(0,payload.indexOf("\r\n\r\n") + 4).toString().split("\n").slice(1); + var headers = {}; + + for (var item in httpHeaderString) { + var parts = httpHeaderString[item].split(":"); + + if (parts.length > 1) { + headers[parts[0]] = parts.slice(1).join(":").trim(); + } + } + + return headers; +} + function httpHeader(payload, name) { var currentLine = 0; var i = 0; @@ -394,7 +409,7 @@ module.exports = { // =========== Tests ============== function testRunner(){ - ["init", "parseMessage", "httpMethod", "httpPath", "setHttpHeader", "httpPathParam", "httpHeader", "httpBody", "setHttpBody", "httpBodyParam", "httpCookie", "setHttpCookie"].forEach(function(t){ + ["init", "parseMessage", "httpMethod", "httpPath", "setHttpHeader", "httpPathParam", "httpHeader", "httpBody", "setHttpBody", "httpBodyParam", "httpCookie", "setHttpCookie", "httpHeaders"].forEach(function(t){ console.log(`====== Start ${t} =======`) eval(`TEST_${t}()`) console.log(`====== End ${t} =======`) @@ -668,3 +683,25 @@ function TEST_setHttpCookie() { return fail(`Should add new cookie: ${p}`) } } + +function TEST_httpHeaders() { + const examplePayload = "GET / HTTP/1.1\r\nHost: localhost:3000\r\nUser-Agent: Node\r\nContent-Length:5\r\n\r\nhello"; + + let expectedHeaders = {"Host": "localhost:3000", "User-Agent": "Node", "Content-Length": "5"} + let payload = Buffer.from(examplePayload); + let headers = httpHeaders(payload); + + ["Host", "User-Agent", "Content-Length"].forEach(function(header){ + let actual = headers[header]; + let expected = expectedHeaders[header]; + + if (!actual) { + fail(`${header} Header was not found`); + } + + if (actual != expected) { + fail(`${header} Header not Equal to Expected: ${expected} was ${actual}`); + } + + }) +}