Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 456
Luke Puplett Avatar asked Dec 21 '22 01:12

Luke Puplett


1 Answers

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.

like image 55
Luke Puplett Avatar answered May 20 '23 14:05

Luke Puplett