Skip to content

Commit 6c9d0e2

Browse files
committed
Added HttpContentHeader class containing common content header names (closes #211)
1 parent 167cb61 commit 6c9d0e2

File tree

4 files changed

+125
-4
lines changed

4 files changed

+125
-4
lines changed

documentation/README.md

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
- Integration testing of the full server pipeline
4545
- [HTTP server](#http-server)
4646
- [OWIN pipeline](#owin-pipeline)
47-
- Additional methods
47+
- Additional classes and methods
48+
- [Helper classes](#helper-classes)
4849
- [AndProvide... methods](#andprovide-methods)
4950

5051
### Using custom HttpConfiguration
@@ -572,7 +573,7 @@ MyWebApi
572573
MyWebApi
573574
.Controller<WebApiController>()
574575
.WithHttpRequestMessage(request => request
575-
.WithHeader("SomeHeader", "SomeHeaderValue"));
576+
.WithHeader(HttpHeader.Accept, MediaType.TextHtml));
576577

577578
// adding custom header with multiple values to the request message
578579
MyWebApi
@@ -591,7 +592,7 @@ MyWebApi
591592
MyWebApi
592593
.Controller<WebApiController>()
593594
.WithHttpRequestMessage(request => request
594-
.WithContentHeader("SomeContentHeader", "SomeContentHeaderValue"));
595+
.WithContentHeader(HttpContentHeader.ContentType, MediaType.ApplicationJson));
595596

596597
// adding custom content header with multiple values to the request message
597598
// * adding content headers requires content to be initialized and set
@@ -2609,6 +2610,57 @@ Summary - the **".Working()"** method without parameters will check if the globa
26092610

26102611
[To top](#table-of-contents)
26112612

2613+
### Helper classes
2614+
2615+
The library gives you helper classes for common magic strings and non-important action call parameters:
2616+
2617+
```c#
2618+
// With.Any<TParameter> is helpful where action call parameter values are not important
2619+
MyWebApi
2620+
.Controller<WebApiController>()
2621+
.Calling(c => c.SomeAction(With.Any<int>()))
2622+
.ShouldHave()
2623+
.ActionAttributes();
2624+
2625+
// MediaType class contains common media type strings
2626+
MyWebApi
2627+
.Controller<WebApiController>()
2628+
.Calling(c => c.SomeAction())
2629+
.ShouldReturn()
2630+
.Content()
2631+
.WithMediaType(MediaType.ApplicationJson); // represents "application/json"
2632+
2633+
// HttpHeader class contains common HTTP header names
2634+
MyWebApi
2635+
.Controller<WebApiController>()
2636+
.WithHttpRequestMessage(
2637+
request => request
2638+
.WithHeader(HttpHeader.Accept, MediaType.ApplicationJson)) // represents "Accept" HTTP header
2639+
.Calling(c => c.SomeAction())
2640+
.ShouldReturn()
2641+
.Ok();
2642+
2643+
// HttpContentHeader class contains common HTTP content header names
2644+
MyWebApi
2645+
.Controller<WebApiController>()
2646+
.WithHttpRequestMessage(
2647+
request => request
2648+
.WithContentHeader(HttpContentHeader.ContentType, MediaType.ApplicationJson)) // represents "ContentType" HTTP header
2649+
.Calling(c => c.SomeAction())
2650+
.ShouldReturn()
2651+
.Ok();
2652+
2653+
// AuthenticationScheme class containing common authentication schemes
2654+
MyWebApi
2655+
.Controller<WebApiController>()
2656+
.Calling(c => c.UnauthorizedActionWithChallenges())
2657+
.ShouldReturn()
2658+
.Unauthorized()
2659+
.ContainingAuthenticationHeaderChallenge(AuthenticationScheme.Basic); // represents Basic authentication scheme
2660+
```
2661+
2662+
[To top](#table-of-contents)
2663+
26122664
### AndProvide... methods
26132665

26142666
You can get different Web API specific objects used in the test case where applicable by using AndProvide... methods.

samples/Books Web API/Books.Tests/ApiTests/IntegrationTests/BooksControllerIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void BooksControllerShouldReturnCorrectBooksForAuthorizedUsers()
5656
.WithHttpRequestMessage(req => req
5757
.WithRequestUri("/api/Books/Get")
5858
.WithMethod(HttpMethod.Get)
59-
.WithHeader("Authorization", "Bearer " + this.accessToken))
59+
.WithHeader(HttpHeader.Authorization, "Bearer " + this.accessToken))
6060
.ShouldReturnHttpResponseMessage()
6161
.WithStatusCode(HttpStatusCode.OK)
6262
.WithResponseModelOfType<List<BookResponseModel>>()

src/MyWebApi/HttpContentHeader.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// MyWebApi - ASP.NET Web API Fluent Testing Framework
2+
// Copyright (C) 2015 Ivaylo Kenov.
3+
//
4+
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
5+
6+
namespace MyTested.WebApi
7+
{
8+
/// <summary>
9+
/// Contains common HTTP content header names.
10+
/// </summary>
11+
public class HttpContentHeader
12+
{
13+
/// <summary>
14+
/// Represents Allow content header name.
15+
/// </summary>
16+
public const string Allow = "Allow";
17+
18+
/// <summary>
19+
/// Represents ContentDisposition content header name.
20+
/// </summary>
21+
public const string ContentDisposition = "ContentDisposition";
22+
23+
/// <summary>
24+
/// Represents ContentEncoding content header name.
25+
/// </summary>
26+
public const string ContentEncoding = "ContentEncoding";
27+
28+
/// <summary>
29+
/// Represents ContentLanguage content header name.
30+
/// </summary>
31+
public const string ContentLanguage = "ContentLanguage";
32+
33+
/// <summary>
34+
/// Represents ContentLength content header name.
35+
/// </summary>
36+
public const string ContentLength = "ContentLength";
37+
38+
/// <summary>
39+
/// Represents ContentLocation content header name.
40+
/// </summary>
41+
public const string ContentLocation = "ContentLocation";
42+
43+
/// <summary>
44+
/// Represents ContentMD5 content header name.
45+
/// </summary>
46+
public const string ContentMD5 = "ContentMD5";
47+
48+
/// <summary>
49+
/// Represents ContentRange content header name.
50+
/// </summary>
51+
public const string ContentRange = "ContentRange";
52+
53+
/// <summary>
54+
/// Represents ContentType content header name.
55+
/// </summary>
56+
public const string ContentType = "ContentType";
57+
58+
/// <summary>
59+
/// Represents Expires content header name.
60+
/// </summary>
61+
public const string Expires = "Expires";
62+
63+
/// <summary>
64+
/// Represents LastModified content header name.
65+
/// </summary>
66+
public const string LastModified = "LastModified";
67+
}
68+
}

src/MyWebApi/MyWebApi.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
<Compile Include="Builders\Contracts\ExceptionErrors\IAggregateExceptionTestBuilder.cs" />
114114
<Compile Include="Builders\Contracts\ExceptionErrors\IAndAggregateExceptionTestBuilder.cs" />
115115
<Compile Include="Builders\Contracts\ExceptionErrors\IBaseExceptionTestBuilder.cs" />
116+
<Compile Include="HttpContentHeader.cs" />
116117
<Compile Include="IHttpMessageHandlerBuilder.cs" />
117118
<Compile Include="Builders\Contracts\Handlers\IInnerHttpMessageHandlerBuilder.cs" />
118119
<Compile Include="Builders\Contracts\HttpActionResults\Ok\IAndOkTestBuilder.cs" />

0 commit comments

Comments
 (0)