From c23f95d4cfea27669c1a7fad18ed9a50ba0acf47 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Wed, 17 May 2017 22:47:05 +0300 Subject: [PATCH] Add httpMethod function --- middleware/middleware.js | 20 +++++++++++++++++++- middleware/package.json | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/middleware/middleware.js b/middleware/middleware.js index 431c384..306c17b 100755 --- a/middleware/middleware.js +++ b/middleware/middleware.js @@ -183,6 +183,11 @@ function searchResponses(id, searchPattern, callback) { // \r\n // hello +function httpMethod(payload) { + var pEnd = payload.indexOf(' '); + return payload.slice(0, pEnd).toString("ascii"); +} + function httpPath(payload) { var pStart = payload.indexOf(' ') + 1; var pEnd = payload.indexOf(' ', pStart); @@ -351,6 +356,7 @@ module.exports = { parseMessage: parseMessage, searchResponses: searchResponses, httpPath: httpPath, + httpMethod: httpMethod, setHttpPath: setHttpPath, httpPathParam: httpPathParam, setHttpPathParam: setHttpPathParam, @@ -371,7 +377,7 @@ module.exports = { // =========== Tests ============== function testRunner(){ - ["init", "parseMessage", "httpPath", "setHttpHeader", "httpPathParam", "httpHeader", "httpBody", "setHttpBody", "httpBodyParam", "httpCookie", "setHttpCookie"].forEach(function(t){ + ["init", "parseMessage", "httpMethod", "httpPath", "setHttpHeader", "httpPathParam", "httpHeader", "httpBody", "setHttpBody", "httpBodyParam", "httpCookie", "setHttpCookie"].forEach(function(t){ console.log(`====== Start ${t} =======`) eval(`TEST_${t}()`) console.log(`====== End ${t} =======`) @@ -451,6 +457,18 @@ function TEST_httpPath() { } } +function TEST_httpMethod() { + const examplePayload = "GET /test HTTP/1.1\r\n\r\n"; + + let payload = Buffer.from(examplePayload); + let method = httpMethod(payload); + + if (method != "GET") { + return fail(`Path '${method}' != 'GET'`) + } +} + + function TEST_httpPathParam() { let p = Buffer.from("GET / HTTP/1.1\r\n\r\n"); diff --git a/middleware/package.json b/middleware/package.json index 463a03f..698165f 100644 --- a/middleware/package.json +++ b/middleware/package.json @@ -1,6 +1,6 @@ { "name": "goreplay_middleware", - "version": "0.1.12", + "version": "0.1.13", "description": "Package for writing middleware for GoReplay https://goreplay.org", "main": "middleware.js", "scripts": {