StatusCode is defined as integer (string is not possible) #227

Closed
opened 2025-12-29 14:25:47 +01:00 by adam · 12 comments
Owner

Originally created by @hsnsalhi on GitHub (Nov 29, 2019).

Hello,
I want to extract the status code from url but the mapping json is skipped because StatusCode can't be as string.
This is an example of what I want to do:

{
    "Request": {
      "Path": {
        "Matchers": [
          {
            "Name": "RegexMatcher",
            "Pattern": "/test/(\\d+)"
          }
        ]
      },
      "Methods": [
        "post"
      ]
    },
    "Response": {
      "StatusCode": "{{Regex.Match request.url \"\\d+\"}}",
      "BodyAsJson": {
        "path": "{{request.path}}"
      },
      "UseTransformer": true
    }
  }
Originally created by @hsnsalhi on GitHub (Nov 29, 2019). Hello, I want to extract the status code from url but the mapping json is skipped because StatusCode can't be as string. This is an example of what I want to do: ``` js { "Request": { "Path": { "Matchers": [ { "Name": "RegexMatcher", "Pattern": "/test/(\\d+)" } ] }, "Methods": [ "post" ] }, "Response": { "StatusCode": "{{Regex.Match request.url \"\\d+\"}}", "BodyAsJson": { "path": "{{request.path}}" }, "UseTransformer": true } } ```
adam added the bug label 2025-12-29 14:25:47 +01:00
adam closed this issue 2025-12-29 14:25:47 +01:00
Author
Owner

@StefH commented on GitHub (Nov 29, 2019):

According to HTTP specification, I think it's only allowed to use a number between 100 and 599 as status code.

@StefH commented on GitHub (Nov 29, 2019): According to HTTP specification, I think it's only allowed to use a number between 100 and 599 as status code.
Author
Owner

@hsnsalhi commented on GitHub (Nov 29, 2019):

Yes, in fact my use case respects this spec (the example was just to show what I need)
But the problem is in the string to number conversion. the Response object does not accept StatusCode to be string, neither the method WithStatusCode in C# code.

@hsnsalhi commented on GitHub (Nov 29, 2019): Yes, in fact my use case respects this spec (the example was just to show what I need) But the problem is in the string to number conversion. the Response object does not accept StatusCode to be string, neither the method WithStatusCode in C# code.
Author
Owner

@StefH commented on GitHub (Nov 29, 2019):

Correct. The reason that you cannot use a string because it should always be an integer. So what issue do you have with this?

@StefH commented on GitHub (Nov 29, 2019): Correct. The reason that you cannot use a string because it should always be an integer. So what issue do you have with this?
Author
Owner

@hsnsalhi commented on GitHub (Nov 29, 2019):

In this case, is there a way to extract the status code from the url ?
example:
I send http://localhost/test/statuscode401
And the response should be with status code 401

@hsnsalhi commented on GitHub (Nov 29, 2019): In this case, is there a way to extract the status code from the url ? example: I send http://localhost/test/statuscode401 And the response should be with status code 401
Author
Owner

@StefH commented on GitHub (Nov 29, 2019):

If you send something like http://localhost/test/statuscode/401, then you can get the 401 using:
request.PathSegments.[2].

However, at this moment, you cannot set this to the StatusCode because only integer is allowed.

This could be implemented with a new cast extension on Handlebars. I can investigate this.
Also allowing a the code to be converted from string to integer could be an option.
(BTW I need to check if the StatusCode is actually processed by Handlebars...)

@StefH commented on GitHub (Nov 29, 2019): If you send something like `http://localhost/test/statuscode/401`, then you can get the 401 using: `request.PathSegments.[2]`. However, at this moment, you cannot set this to the StatusCode because only integer is allowed. This could be implemented with a new cast extension on Handlebars. I can investigate this. Also allowing a the code to be converted from string to integer could be an option. (BTW I need to check if the StatusCode is actually processed by Handlebars...)
Author
Owner

@StefH commented on GitHub (Nov 29, 2019):

BTW just posting a new mapping to the __admin is possible with a string as StatusCode:

{
    "Request": {
      "Path": {
        "Matchers": [
          {
            "Name": "RegexMatcher",
            "Pattern": "/statuscode/(\\d+)"
          }
        ]
      },
      "Methods": [
        "post"
      ]
    },
    "Response": {
      "StatusCode": "401",
      "BodyAsJson": {
        "path": "{{request.path}}"
      },
      "UseTransformer": true
    }
}
@StefH commented on GitHub (Nov 29, 2019): BTW just posting a new mapping to the __admin is possible with a string as StatusCode: ``` js { "Request": { "Path": { "Matchers": [ { "Name": "RegexMatcher", "Pattern": "/statuscode/(\\d+)" } ] }, "Methods": [ "post" ] }, "Response": { "StatusCode": "401", "BodyAsJson": { "path": "{{request.path}}" }, "UseTransformer": true } } ```
Author
Owner

@hsnsalhi commented on GitHub (Nov 29, 2019):

Actually it's the workaround I'm working with.
I create a mapping for every possible status code returned by the mocked API
Thank you for your help

@hsnsalhi commented on GitHub (Nov 29, 2019): Actually it's the workaround I'm working with. I create a mapping for every possible status code returned by the mocked API Thank you for your help
Author
Owner

@StefH commented on GitHub (Nov 30, 2019):

@hsnsalhi
Can you try WireMock.Net.1.0.37-ci-12253.nupkg from MyGet ? (https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)

@StefH commented on GitHub (Nov 30, 2019): @hsnsalhi Can you try `WireMock.Net.1.0.37-ci-12253.nupkg` from MyGet ? (https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)
Author
Owner

@StefH commented on GitHub (Dec 3, 2019):

@hsnsalhi Can you try if this version solves your issue?

@StefH commented on GitHub (Dec 3, 2019): @hsnsalhi Can you try if this version solves your issue?
Author
Owner

@StefH commented on GitHub (Dec 5, 2019):

@hsnsalhi Can you please test if that version solves your problem?

@StefH commented on GitHub (Dec 5, 2019): @hsnsalhi Can you please test if that version solves your problem?
Author
Owner

@StefH commented on GitHub (Dec 10, 2019):

@hsnsalhi Can you please test if that version solves your problem?

@StefH commented on GitHub (Dec 10, 2019): @hsnsalhi Can you please test if that version solves your problem?
Author
Owner

@StefH commented on GitHub (Dec 17, 2019):

@hsnsalhi Can you please test?

@StefH commented on GitHub (Dec 17, 2019): @hsnsalhi Can you please test?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#227