question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't detect non present header fields

See original GitHub issue

I was trying to do bad/missing header field detection. I could detect the values of a header fine when it was present, however, I can’t seem to get anything to trigger when it’s not present. I lifted the below api-key check from the Wiki, but for me this doesn’t ever trigger.

{
        "Swagger Doc Title": "Bad Request",
        "Comment": "Bad JWT token",
        "Priority": 1,
        "Request": {
            "Path": {
                "Matchers": [
                    {
                        "Name": "ExactMatcher",
                        "Pattern": "/timeEntry"
                    }
                ]
            },
            "Headers": [
                {
                    "Name": "api-key",
                    "Matchers": [
                        {
                            "Name": "WildcardMatcher",
                            "Pattern": "*",
                            "IgnoreCase": true,
                            "RejectOnMatch": true
                        }
                    ]
                }
            ]
        },
        "Response": {
            "StatusCode": 400,
            "Headers": {
                "Content-Type": "application/json"
            },
            "BodyAsJson": {
                "errorMessage": "Validation exception",
                "status": 400,
                "WireMock reason": "Bad Authorization bearer token"
            },
            "UseTransformer": false
        }
    }

Log output for sample request without this Header field provided.

{
  "Guid": "a5054f98-75e7-4b04-b110-0ed2e53b3ece",
  "Request": {
    "ClientIP": "::1",
    "DateTime": "2023-02-16T14:03:06.5702065Z",
    "Path": "/timeEntry",
    "AbsolutePath": "/timeEntry",
    "Url": "http://localhost:9091/timeEntry",
    "AbsoluteUrl": "http://localhost:9091/timeEntry",
    "ProxyUrl": null,
    "Query": {},
    "Method": "POST",
    "Headers": {
      "Postman-Token": [
        "89b82b0b-8fa9-4185-8240-945029798d16"
      ],
      "Connection": [
        "keep-alive"
      ],
      "Content-Length": [
        "54"
      ],
      "Content-Type": [
        "application/json"
      ],
      "Accept": [
        "*/*"
      ],
      "Accept-Encoding": [
        "gzip, deflate, br"
      ],
      "Host": [
        "localhost:9091"
      ],
      "User-Agent": [
        "PostmanRuntime/7.31.0"
      ]
    },
    "Cookies": {},
    "Body": "{\n  \"username\": \"username\",\n  \"password\": \"password\"\n}",
    "BodyAsJson": {
      "username": "username",
      "password": "password"
    },
    "BodyAsBytes": null,
    "BodyEncoding": {
      "CodePage": 65001,
      "EncodingName": "Unicode (UTF-8)",
      "WebName": "utf-8"
    },
    "DetectedBodyType": "Json",
    "DetectedBodyTypeFromContentType": "Json"
  },
  "Response": {
    "StatusCode": 404,
    "Headers": {
      "Content-Type": [
        "application/json"
      ]
    },
    "BodyDestination": null,
    "Body": null,
    "BodyAsJson": {
      "Guid": null,
      "Status": "No matching mapping found"
    },
    "BodyAsBytes": null,
    "BodyAsFile": null,
    "BodyAsFileIsCached": null,
    "BodyOriginal": null,
    "BodyEncoding": null,
    "DetectedBodyType": 2,
    "DetectedBodyTypeFromContentType": null,
    "FaultType": null,
    "FaultPercentage": null
  },
  "MappingGuid": null,
  "MappingTitle": null,
  "RequestMatchResult": null,
  "PartialMappingGuid": "35eda15f-6568-4a71-8fb7-a98d2eea1be4",
  "PartialMappingTitle": null,
  "PartialRequestMatchResult": {
    "TotalScore": 3.0,
    "TotalNumber": 4,
    "IsPerfectMatch": false,
    "AverageTotalScore": 0.75,
    "MatchDetails": [
      {
        "Name": "PathMatcher",
        "Score": 0.0
      },
      {
        "Name": "MethodMatcher",
        "Score": 1.0
      },
      {
        "Name": "HeaderMatcher",
        "Score": 1.0
      },
      {
        "Name": "BodyMatcher",
        "Score": 1.0
      }
    ]
  }
}

Issue Analytics

  • State:closed
  • Created 7 months ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
StefHcommented, Mar 23, 2023

@MichaelIDS I did some testing and this works, but you have to configure the mapping for the header in a different way.

It should be:

"Headers": [
                {
                    "Name": "api-key",
                    "IgnoreCase": true,
                    "RejectOnMatch": true
                }
            ]

This means that when the header-key (“api-key”) or “API-Key” (ignorecase = true) is missing the header mapping will match because RejectOnMatch is true.

I’ll add this to the wiki.

0reactions
MichaelIDScommented, Mar 23, 2023

In the log it has the path captured and it looks identical to the stub definition in the original message here to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to check not exist header field with supertest?
I would suggest creating custom validation as follows: it('should validate header is not present.', function(done){ request .get ...
Read more >
can't access to response header fields although OPTIONS ...
In my browser I can inspect the header and I can see that all the fields that I need are present(such as token,...
Read more >
HTTP Error 431: 3 Ways to Fix Request Header Fields Too ...
Follow this tutorial to learn three methods to fix the HTTP Error 431: Request Header Fields Too Large response status code.
Read more >
HTTP/1.1: Header Field Definitions
If no Accept header field is present, then it is assumed that the client accepts all media types. If an Accept header field...
Read more >
HTTP headers - MDN Web Docs - Mozilla
HTTP headers let the client and the server pass additional information with an HTTP request or response. An HTTP header consists of its...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found