Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Mock an HTTP request in a unit testing scenario in Python
    primarykey
    data
    text
    <p>I would like to include a Web server for all my test related to HTTP. It doesn't need to be very sophisticated. I would prefer not to be dependent of being online. So I could test some options of my program. </p> <ol> <li>Start the server </li> <li>Create a few resources (URI) with appropriate mime types, response code, etc.</li> <li>Run the tests (would be good to not have to start the server for each tests too)</li> <li>Shut down the server.</li> </ol> <p>Any hints on this code would be helpful. I tried a few things with BaseHTTPServer but not successful yet. nosetests command seems to wait indefinitely.</p> <pre><code>import unittest from foo import core class HttpRequests(unittest.TestCase): """Tests for HTTP""" def setUp(self): "Starting a Web server" self.port = 8080 # Here we need to start the server # # Then define a couple of URIs and their HTTP headers # so we can test the code. pass def testRequestStyle(self): "Check if we receive a text/css content-type" myreq = core.httpCheck() myuri = 'http://127.0.0.1/style/foo' myua = "Foobar/1.1" self.asserEqual(myreq.mimetype(myuri, myua), "text/css") def testRequestLocation(self): "another test" pass def tearDown(self): "Shutting down the Web server" # here we need to shut down the server pass </code></pre> <p>thanks for any help.</p> <hr> <p><strong>Update - 2012:07:10T02:34:00Z</strong></p> <p>This is a code which for a given Web site will return the list of CSS. I want to test if it returns the right list of CSS.</p> <pre><code>import unittest from foo import core class CssTests(unittest.TestCase): """Tests for CSS requests""" def setUp(self): self.css = core.Css() self.req = core.HttpRequests() def testCssList(self): "For a given Web site, check if we get the right list of linked stylesheets" WebSiteUri = 'http://www.opera.com/' cssUriList = [ 'http://www.opera.com/css/handheld.css', 'http://www.opera.com/css/screen.css', 'http://www.opera.com/css/print.css', 'http://www.opera.com/css/pages/home.css'] content = self.req.getContent(WebSiteUri) cssUriListReq = self.css.getCssUriList(content, WebSiteUri) # we need to compare ordered list. cssUriListReq.sort() cssUriList.sort() self.assertListEqual(cssUriListReq, cssUriList) </code></pre> <p>Then in <code>foo/core.py</code></p> <pre><code>import urlparse import requests from lxml import etree import cssutils class Css: """Grabing All CSS for one given URI""" def getCssUriList(self, htmltext, uri): """Given an htmltext, get the list of linked CSS""" tree = etree.HTML(htmltext) sheets = tree.xpath('//link[@rel="stylesheet"]/@href') for i, sheet in enumerate(sheets): cssurl = urlparse.urljoin(uri, sheet) sheets[i] = cssurl return sheets </code></pre> <p>Right now, the code depends on an online server. It should not. I want to be able to add plenty of different types of combination of stylesheets and to test the protocol and then later on some options on their parsing, combinations, etc.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload