0

I wrote a simple webservice for uploading a file.

<%@ WebService Language="C#" class="AppWebService" %>

using System;
using System.Web.Services;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.IO;


[WebService(Namespace="http://myip/services")]
public class AppWebService : WebService
{    
    [WebMethod]
    public string UploadFile(byte[] f, string fileName)
    {
        // the byte array argument contains the content of the file
        // the string argument contains the name and extension
        // of the file passed in the byte array
        try
        {
            // instance a memory stream and pass the
            // byte array to its constructor
            MemoryStream ms = new MemoryStream(f);

            // instance a filestream pointing to the 
            // storage folder, use the original file name
            // to name the resulting file
            FileStream fs = new FileStream
                (System.Web.Hosting.HostingEnvironment.MapPath("/TransientStorage/") +
                fileName, FileMode.Create);

            // write the memory stream containing the original
            // file as a byte array to the filestream
            ms.WriteTo(fs);

            // clean up
            ms.Close();
            fs.Close();
            fs.Dispose();

            // return OK if we made it this far
            return "OK";
        }
        catch (Exception ex)
        {
            // return the error message if the operation fails
            return ex.Message.ToString();
        }
    }




    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

}

Now I am trying to test the functionality but am having trouble interacting with the webservice via C#. I've searched around tried using HTTPWebrequest (multipart/form-data) that I found in this post but didn't have much success and am not sure that this is the right approach.

How can I test the webservice that I wrote to see if I can successfully upload a file?

Community
  • 1
  • 1
Nick
  • 9,285
  • 33
  • 104
  • 147
  • 1
    Did you try writing a unit test against the web service method? – Siva Aug 08 '13 at 18:04
  • That is the main part of my question. I'm trying to figure out how to test my webservice via test code. – Nick Aug 08 '13 at 18:06
  • 1
    If you want automated testing in general, look into [Selenium](http://docs.seleniumhq.org/) or [WatiN](http://watin.org/). They can make real requests and test web services, as well as normal pages. – voithos Aug 08 '13 at 18:09
  • 1
    ASMX is a legacy technology, and should not be used for new development. WCF should be used for all new development of web service clients and servers. One hint: Microsoft has retired the [ASMX Forum](http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/threads) on MSDN. – John Saunders Aug 08 '13 at 18:19
  • FYI, your two streams need to be in `using` blocks, and you don't need `.ToString()` since `Message` is already a string, and if you want to return exception details, use `ex.ToString()`. – John Saunders Aug 08 '13 at 18:50
  • and I am thinking System.Web.Hosting.HostingEnvironment.MapPath is not what you want. I believe that returns http and not C:\ – phillip Aug 08 '13 at 19:38

2 Answers2

1

Are you looking to write test cases or just run some tests via curl or a ui

Could use WCF Test Client Could use curl

Here is a link to some code which should help as well.

dre
  • 1,422
  • 3
  • 21
  • 31
1

One easy to test the code is to create unit test by right clicking on method you want to test and select Create Unit Tests. You will have a test method stub generated for you with all required variables initialized to null. Initialize all the variables with the data you want and run the unit tests. This will test the method itself.
I am not sure if this is what you are looking for.

Siva
  • 396
  • 6
  • 12
  • That's an easy way, but it's not a great way. It also doesn't design the unit tests for you. – John Saunders Aug 08 '13 at 18:22
  • @JohnSaunders Yes, I didn't say that this is the best way, but an easy one and a good place to start and debug through the code. – Siva Aug 08 '13 at 18:27
  • @JohnSaunders a bit harsh. certainly doesn't inspire people to answer. – phillip Aug 08 '13 at 18:54
  • @JohnSaunders: Can you please elaborate on "It doesn't design the unit tests". When I create a unit test, it creates all the required stubs and I just have to update the variables to pass in the right values. – Siva Aug 08 '13 at 18:55
  • @Siva +1 from me just cause JohnSaunders is being mean. – phillip Aug 08 '13 at 18:56
  • 1
    What are the "right values"? How many tests do you need? What should each one test? None of that is done for you. – John Saunders Aug 08 '13 at 18:58
  • No auto-generated code ever creates values to be passed in(at least to what I have seen). It is user's responsibility to pass in the values to run a positive or a negative test and assert the results accordingly. You will need at least one test to test for happy path situation, anything more is on user's discretion. Anyways, not to digress, the answer answers to what was asked(to test the functionality by interacting with Web Service via C#). – Siva Aug 08 '13 at 19:04