Click here to Skip to main content
13,091,300 members (48,839 online)
Rate this:
Please Sign up or sign in to vote.
hi there,

i'm writing an application that is getting some data from the web over a proxy. as core function i use the simple-http-proxy-class from this site. (can be seen below)

as my app is runnning only somehow and i don't know if everything is going as i want it to, i researched the web and i came accross unit testing. i already implemented many tests, but have absolutly no idea how to test something that would need imput from an external source.

i found some solutions on the web, but there's no solution, that is testing a "proxified" connection.

actually i don't want to test this class, but i want somebody to help me with this, so is understand how it would work.


the simple http class:
class simplehttp
 public string geturl(string url, string proxyip, int port, string proxylogin, string proxypassword)
  HttpWebResponse resp;
  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  req.UserAgent = "Mozilla/5.0";
  req.AllowAutoRedirect = true;
  req.ReadWriteTimeout = 5000;
  req.CookieContainer = new CookieContainer();
  req.Referer = "";
  req.Headers.Set("Accept-Language", "en,en-us");
  StreamReader stream_in;
  WebProxy proxy = new WebProxy(proxyip, port);
  //if proxylogin is an empty string then don't use proxy credentials (open proxy)
  if (proxylogin != "") proxy.Credentials = new NetworkCredential(proxylogin, proxypassword);
  req.Proxy = proxy;
  string response = "";
   resp = (HttpWebResponse)req.GetResponse();
   stream_in = new StreamReader(resp.GetResponseStream());
   response = stream_in.ReadToEnd();
  catch (Exception ex)
  return response;


i already tried to make a local proxy and a local webserver and perfom something like an integration test.
its working, if the:
1. proxy is correct and url is correct --> behaves as ecpected
2. proxy is correct and url is incorrect --> behaves as expected
3. proxy is incorrect and url is correct --> don't behaves as expected. the url content is returned by the function even if the port thats should be used by the function is no local proxy

i have no idea, why the function returns the url content with this setup.
Posted 21-Dec-11 1:39am
Updated 21-Dec-11 1:49am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The proxy typically has a different port than the service you are trying to consume.

Here are some improvements you should consider:
* SimpleHttp is no real "class" in object-orientated manner. You have simply a class that contains a method. Better: Provide an IStringDataProvider Interface which could look like:
public interface IStringDataProvider
  string GetContent();

Then you are able to get the content from other sources than http - and your testing will be easier as you can replace it with a mock-provider. The url can be passed to the http-provider-instance with dependency injection (see code below).

* GetUrl sounds like "Hey, get me the url!". Better: GetContent or GetContentByUrl
* Dont pass all the proxy-propertys on each request. Better: Set the WebRequest.DefaultWebProxy at application startup. Then you are independent of using a proxy or not and your whole application will use the same proxy.

* Dont swallow exceptions! Either you have a TryGetContent-Method returning whether the operation succeeded or you handle the exception when you want to check whether an error occured - this is typically done in the GUI.

Putting all together:
interface IStringDataProvider
    string GetContent();
class HttpStringDataProvider : IStringDataProvider
    public HttpStringDataProvider(string url) //The url is passed with dependency injection i.e. through parameters on construction
        Url = url;
    public string Url { get; private set; }
    private CookieContainer cookies = new CookieContainer();
    public string GetContent()
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
        request.UserAgent = "Mozilla/5.0";
        request.AllowAutoRedirect = true;
        request.ReadWriteTimeout = 5000;
        request.CookieContainer = cookies;
        request.Referer = "";
        request.Headers.Set("Accept-Language", "en,en-us");
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader streamReader = new StreamReader(response.GetResponseStream());
        return streamReader.ReadToEnd();

On startup:
if (proxyType == "http")
    WebProxy proxy = new WebProxy(myProxyHost, myProxyPort);
    proxy.Credentials = new NetworkCredential(myProxyUsername, myProxyPassword);
    WebRequest.DefaultWebProxy = proxy; //This proxy will be used by all HttpWebRequests if their Proxy-Property is not set.
else if (proxyType == "system")
    WebRequest.DefaultWebProxy = WebRequest.GetSystemWebProxy(); //Settings from Internet Explorer (default)
else if (proxyType == "no")
    WebRequest.DefaultWebProxy = null; //This will speed up the whole a lot, because the default is the proxy used by IE and otherwise the .Net-Framework tries to gather the IE-proxy settings on each request which is very slow

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 21 Dec 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100