Skip to content

Commit

Permalink
Merge pull request #118 from leandrodasilvaalves/master
Browse files Browse the repository at this point in the history
Include headers in verification endpoint
  • Loading branch information
natenho authored Jul 6, 2023
2 parents 238049b + bcc9082 commit cd870c5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ IOptions<MockacoOptions> options
{
Route = httpContext.Request.Path.Value,
Timestamp = $"{DateTime.Now.ToString("t")}",
Headers = LoadHeaders(httpContext, options.Value.VerificationIgnoredHeaders),
Body = await httpContext.Request.ReadBodyStream()
}, DateTime.Now.AddMinutes(options.Value.MatchedRoutesCacheDuration));

Expand Down Expand Up @@ -109,5 +110,12 @@ private async Task LogHttpContext(HttpContext httpContext)
_logger.LogDebug("Body: {body}", body);
}
}

private static IEnumerable<object> LoadHeaders(HttpContext httpContext, IEnumerable<string> verificationIgnoredHeaders)
{
return from header in httpContext.Request.Headers.ToList()
where !verificationIgnoredHeaders.Any(opt => opt == header.Key)
select new { header.Key, Value = header.Value[0] };
}
}
}
2 changes: 2 additions & 0 deletions src/Mockaco.AspNetCore/Options/MockacoOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class MockacoOptions

public List<string> References { get; set; }

public List<string> VerificationIgnoredHeaders { get; set; }

public List<string> Imports { get; set; }

public int MatchedRoutesCacheDuration { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions src/Mockaco.AspNetCore/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Mockaco.MockacoOptions.DefaultHttpStatusCode.get -> System.Net.HttpStatusCode
Mockaco.MockacoOptions.DefaultHttpStatusCode.set -> void
Mockaco.MockacoOptions.ErrorHttpStatusCode.get -> System.Net.HttpStatusCode
Mockaco.MockacoOptions.ErrorHttpStatusCode.set -> void
Mockaco.MockacoOptions.VerificationIgnoredHeaders.get -> System.Collections.Generic.List<string>
Mockaco.MockacoOptions.VerificationIgnoredHeaders.set -> void
Mockaco.MockacoOptions.Imports.get -> System.Collections.Generic.List<string>
Mockaco.MockacoOptions.Imports.set -> void
Mockaco.MockacoOptions.MatchedRoutesCacheDuration.get -> int
Expand Down
10 changes: 10 additions & 0 deletions src/Mockaco/Settings/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
"ErrorHttpStatusCode": "NotImplemented",
"DefaultHttpContentType": "application/json",
"References": [],
"VerificationIgnoredHeaders": [
"Accept",
"Connection",
"Host",
"User-Agent",
"Accept-Encoding",
"Postman-Token",
"Content-Type",
"Content-Length"
],
"Imports": [],
"MatchedRoutesCacheDuration": 60,
"MockacoEndpoint": "_mockaco",
Expand Down
44 changes: 44 additions & 0 deletions website/docs/verification/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,50 @@ the verification endpoint called in the following way: ```http://localhost:5000/

Both JSON body and x-www-form-urlencoded body are supported.

### Verify request with headers
If you have just called ```http://localhost:5000/hello/Jane Doe```, with the following headers:
```
x-user-id:[email protected]
Authorization:some-bearer-token
endtoend:b9802abd-106f-4d50-b68e-de3198777456
```
the verification endpoint called in the following way: ```http://localhost:5000/_mockaco/verification?route=/hello/Jane Doe``` will respond like so:
```
{
"route": "/hello/Jane Doe",
"timestamp": "14:41",
"headers": [
{
"key": "x-user-id",
"value": "[email protected]"
},
{
"key": "Authorization",
"value": "some-bearer-token"
},
{
"key": "endtoend",
"value": "b9802abd-106f-4d50-b68e-de3198777456"
}
],
"body": ""
}
```

### Configure hidden headers in verification endpoint
You can configure to not display headers that are not relevant to your test. By default the following headers will not be displayed: ```Accept, Connection, Host, User-Agent, Accept-Encoding, Postman-Token, Content-Type, Content-Length.```

```
"Mockaco": {
...
"VerificationIgnoredHeaders": [
"Postman-Token",
"Some-Irrelevant-Header",
],
...
},
```


## Configure custom name of verification endpoint

Expand Down

0 comments on commit cd870c5

Please sign in to comment.