mccalltd/AttributeRouting — Issue #51: Possible testing bug with AR and HttpMethod?
Author: PureKrome
Created: 2012-02-23 05:03:29+00:00
State: open

Hi Tim,

I've been playing with AR in some xUnit tests/facts .. and i've had some problems with trying to retrieve a list of routes for a particular context.

eg. ```routes.GetRouteData(httpContext);```

So after debugging a lot, i finally came across this bit of AR code..

```c#
class: mvcExtensions, line 14

public static string GetHttpMethod(this HttpRequestBase request)
{
return request.SafeGet(r => r.Headers["X-HTTP-Method-Override"]) ??
request.SafeGet(r => r.GetFormValue("X-HTTP-Method-Override")) ??
request.SafeGet(r => r.GetQueryStringValue("X-HTTP-Method-Override")) ??
request.SafeGet(r => r.HttpMethod, "GET");
}
```

Now, becuase i'm in a TEST class, I need to mock my context stuff, like the request. Kewl. What I wasn't (read: forgot) to mock was the HttpMethod property. When I mocked that property to return "GET", then everything worked again. Kewl :)

So my question is this .. should that last line in the above code snippet do a check for ```string.IsNullOrEmpty(HttpMethod) ? "GET" : HttpMethod``` ?

(What made me lead to this was that I notice the stack trace was giving me a First Chance Null Exception in AR .. somewhere .. and i -think- this is where it might have happened?)

Or maybe i've not set up my AR attribute(s) right?

Here's a sample attribute :-

```c#
[GET("1.0/location")]
public JsonResult .....
```

so yeah .. thoughts?