Test case for WCF REST Service

229 views Asked by At

How should I write a Integration test for WCF REST service?

I want to write a test case for POST request, The message contract it's expecting is some complex type.

My Contract

[OperationContract]
[WebInvoke(UriTemplate = "Receiver/Add", Method = "POST", RequestFormat =   WebMessageFormat.Json)]
AccountCreateResponse AddReceiver(AccountCreateRequest request);

My test case is like this

string jsonRequestData = "{\"ReceiverAccountCreateRequest\":{\"FriendlyName\":\"aBC\",\"AccountNumber\":\"11111111111111111\",\"AccountType\":\"1\",\"CategoryName\":\"ACH\",\"IsTemporary\":\"false\",\"ExtendedProperties\":{\"SerializedInstance\":\"Corporate Type~Payroll~False|Batch Description~Dir Dep~False|Company Name~4not4~False|Identification Number~123456789~False|Total Amount~232~False|Direction~C~False|chach Format~PPD~False\"},\"Imported\":\"0\"}}";

byte[] data = Encoding.UTF8.GetBytes(xmlRequestData);
var request = (HttpWebRequest)WebRequest.Create("http://localhost/abcService.svc/Receiver/Add");
request.Method = "POST";
request.ContentType = "application/json";
Stream dataStream = request.GetRequestStream();
dataStream.Write(data, 0, data.Length);
dataStream.Close();

var response = request.GetResponse();

Error

415 Request-content not match.

1

There are 1 answers

1
Akif Patel - BRMS On

You may create Microsoft Unit Test project from the available test templates and add Web Reference of your WCF Service.

Here, better to test using service channel however, you can still use conventional service invoking methods. This is what we have been doing to test various services on several servers since our object model is just too much complex.

follow this link for Unit Test template in VS2013

How to: Create a Unit Test Project