views:

1698

answers:

5

The test form generated by ASMX is pretty handy for testing operations. However, there is no apparent way to include SOAP headers.

How can you test your headers without programming a client to use the service?

A: 

Instead of directly accessing the headers, provide an abstraction that your code accesses instead. For example, if you have a header called "Customer", you might provide a context class you can access like so:

string customer = MyContext.Current.Customer;

Now, all you have to do is swap in a mock implementation in your test classes that doesn't require all the plumbing.

Note, however, that testing ASMX outside of a web server isn't exactly ideal since it can miss things like serialization. If you could deploy to a test server and test the deployed copy, you would be better off. If you really care about testing, WCF is a better option since you can self host WCF rather easily in tests.

jezell
A: 

If I understand what you are trying to do, you can can just do an HTTP request using the SOAP that is provided by your asmx. If you open your asmx in the broswer, you get a list of methods available in your web service. Click on the method you want to test and you will get a SOAP request you can use, just fill in the values you want to test. Below is the code you can use to test the SOAP.

// Set SOAP Message
string msg = "<?xml version='1.0' encoding='UTF-8'?><soap:Envelope>";
...
...

// Make http request
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://linktoyour/service.asmx");

req.Headers.Add("SOAPAction", "http://linktoyour/NameOfFuntion");

req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(msg);

req.ContentLength = bytes.Length;

System.IO.Stream st = req.GetRequestStream();
st.Write(bytes,0,bytes.Length);
st.Close();

// Read response
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
System.IO.Stream st1 = res.GetResponseStream();

System.IO.StreamReader sr = new System.IO.StreamReader(st1, System.Text.Encoding.UTF8);

string txt = sr.ReadToEnd();

// Display response
Response.Write(txt);
Austin
-1 for missing "using" blocks.
John Saunders
+1 because I don't think missing the using blocks was worth the penalty.
Cobus Kruger
+2  A: 

If you care about interop, don't use .net client apps to test .net web services. Use a proper tool like SOAPUI to test your web service. www.soapui.org

The tool is written in Java, but it is free and darn handy for testing any kind of web service.

+1  A: 

I use this open source .net app to test my web services(including ones with soap headers) http://storm.codeplex.com. If you will be looking into WCF services in the near future WCFStorm might also prove useful.

+1  A: 

You asked

How can you test your headers without programming a client to use the service?

The answer is that you should program a client to use the service.

The developers who will write the code to consume your service are going to be forced to write a client that uses your badly-designed, hard-to-use service with its strange headers. It's better you find out about {badly-designed, hard-to-use, strange} before they do.

That way, you can redesign the service to be {well-designed, pleasure-to-use, normal}.

BTW, writing unit tests for your web service is a good way to do this. That way, you've got small, simple, clients.

John Saunders