Skip to content

Commit

Permalink
[#543] Test resource proxy using httpretty
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Mar 2, 2013
1 parent 15c23b2 commit d2ef6c8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 58 deletions.
43 changes: 0 additions & 43 deletions ckanext/resourceproxy/tests/file_server.py

This file was deleted.

46 changes: 31 additions & 15 deletions ckanext/resourceproxy/tests/test_proxy.py
@@ -1,5 +1,7 @@
import requests
import unittest
import json
from httpretty import HTTPretty, httprettified

This comment has been minimized.

Copy link
@vitorbaptista

vitorbaptista Mar 2, 2013

Contributor

Could you change "from ... import ..." to "import ...", as per our coding standards?

This comment has been minimized.

Copy link
@domoritz

domoritz Mar 3, 2013

Author Contributor

Done in ba42dfd. Even though I don't think it doesn't matter in this case. However, I'll do it so that the code is consistent and other people can use the code style as an example. I will deal with this as I do with crossing the street when there is a red traffic light but no car. I won't cross the street if there are children because I don't want to give a poor example.

This comment has been minimized.

Copy link
@tobes

tobes Mar 4, 2013

Contributor

Yeah it's fine for external libs just nothing internal

This comment has been minimized.

Copy link
@vitorbaptista

vitorbaptista Mar 4, 2013

Contributor

That's a great way to think about it :P


import paste.fixture
from pylons import config
Expand All @@ -10,9 +12,15 @@
import ckan.plugins as plugins
import ckan.lib.create_test_data as create_test_data
import ckan.config.middleware as middleware
import ckanext.resourceproxy.controller as controller

import ckanext.resourceproxy.plugin as proxy
import file_server


JSON_STRING = json.dumps({
"a": "foo",
"bar": "yes, I'm proxied",
"b": 42})


class TestProxyBasic(tests.WsgiAppCase, unittest.TestCase):
Expand All @@ -26,12 +34,6 @@ def setup_class(cls):
wsgiapp = middleware.make_app(config['global_conf'], **config)
cls.app = paste.fixture.TestApp(wsgiapp)

if not cls.serving:
file_server.serve()
cls.serving = True
# gets shutdown when nose finishes all tests,
# so don't restart ever

# create test resource
create_test_data.CreateTestData.create()

Expand Down Expand Up @@ -62,21 +64,29 @@ def set_resource_url(self, url):

self.data_dict = {'resource': resource, 'package': package}

@httprettified
def test_resource_proxy_on_200(self):
self.set_resource_url('http://0.0.0.0:50001/test.json')
url = 'http://www.ckan.org/static/test.json'
HTTPretty.register_uri(
HTTPretty.GET, url,
content_type='application/json',
body=JSON_STRING)
self.set_resource_url(url)

url = self.data_dict['resource']['url']
result = requests.get(url)
assert result.status_code == 200, result.status_code
assert "yes, I'm proxied" in result.content, result.content

proxied_url = proxy.get_proxified_resource_url(self.data_dict)
result = self.app.get(proxied_url, status='*')
assert result.status == 200, result.status
assert "yes, I'm proxied" in result.body, result.body

@httprettified
def test_resource_proxy_on_404(self):
self.set_resource_url('http://0.0.0.0:50001/foo.bar')
url = 'http://www.ckan.org/static/test.json'
HTTPretty.register_uri(
HTTPretty.GET, url,
body="I'm not here",
content_type='application/json',
status=404)
self.set_resource_url(url)

url = self.data_dict['resource']['url']
result = requests.get(url)
Expand All @@ -86,8 +96,14 @@ def test_resource_proxy_on_404(self):
result = self.app.get(proxied_url, status='*')
assert result.status == 404, result.status

@httprettified
def test_large_file(self):
self.set_resource_url('http://0.0.0.0:50001/huge.json')
url = 'http://www.ckan.org/static/huge.json'
HTTPretty.register_uri(
HTTPretty.GET, url,
content_length=controller.MAX_FILE_SIZE + 1,
body=JSON_STRING)
self.set_resource_url(url)

proxied_url = proxy.get_proxified_resource_url(self.data_dict)
result = self.app.get(proxied_url, status='*')
Expand Down
1 change: 1 addition & 0 deletions pip-requirements-test.txt
@@ -1,5 +1,6 @@
# These are packages that required when running ckan tests

nose==1.2.1
httpretty==0.5
-e git+https://github.com/okfn/ckanclient#egg=ckanclient

0 comments on commit d2ef6c8

Please sign in to comment.