Luke Puplett
Luke Puplett

Reputation: 45205

How do I set the HttpConfiguration on an HttpRequestMessage for testing?

My controller makes use of CreateResponse on the request object. So in order to test my controller I need to set an instance of HttpConfiguration on the HttpRequestMessage because the CreateResponse expects this configuration to be there.

Nowadays, to aid with testing, there are setters for all sorts of properties but on HttpRequestMessage there is only a GetConfiguration method and no apparent setter.

How do I do this?

Upvotes: 4

Views: 3019

Answers (1)

Luke Puplett
Luke Puplett

Reputation: 45205

Use the following code:

...

ThingController controller = new ThingController(... dependencies ...);

// Fake the configuration.
//
var httpConfig = new HttpConfiguration();
controller.Configuration = httpConfig;

// Fake the request.
//
var httpRequest = new HttpRequestMessage(HttpMethod.Get, "http://mstest/things/1");
httpRequest.Properties[HttpPropertyKeys.HttpConfigurationKey] = httpConfig;

controller.Request = httpRequest;

Note the line 2nd from bottom. Sneaky.

Upvotes: 11

Related Questions