1

I am writing integration test cases where I need to mock web service. I will have a dictionary that maps requests to fake responses. The IEqualityComparer<HttpRequestMessage> is suitably overloaded to treat similar requests as same. To populate the dictionary, I am planning to store request messages in raw format in test files. Copying sample file content below:

POST https://example.com/api/users HTTP/1.1
Prefer: return=representation
Authorization: Bearer BearerToken
Content-Type: application/json; IEEE754Compatible=true
Host: example.com
Content-Length: 23
Expect: 100-continue
Accept-Encoding: gzip, deflate

{"fullname":"John Doe"}

Is it possible to translate the same into HttpRequestMessage object? I do not want to use string as key for dictionary since that would cause false alarms when request message does not significantly differ. Please note that I do not have an option to add a new NuGet package to the project.

Jatin Sanghvi
  • 1,928
  • 1
  • 22
  • 33
  • You would need to manually parse the above string and create your object, making this too broad of a question in its current state. Try parsing it and if you get a more narrowed issue maybe we can help. – Nkosi Dec 25 '17 at 11:56
  • Thanks @Nkosi, I will take it that there's no direct way to get this done using core .NET libraries.The System.Net.Http assembly should be parsing the incoming stream into `HttpRequestMessage` object at server. I thought if there's a way to access similar functionality from outside. – Jatin Sanghvi Dec 25 '17 at 11:59
  • I have done something similar in the past which might be worth a look https://github.com/dkackman/FakeHttp – dkackman Dec 25 '17 at 12:01
  • @JatinSanghvi in that case I suggest you check their source repo on Github and see if you can find it. – Nkosi Dec 25 '17 at 12:42

0 Answers0