Skip to content

Commit

Permalink
Merge branch '543-httpretty-for-proxy-tests'
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Mar 20, 2013
2 parents c087d25 + 54c932a commit 4bf6bcd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 79 deletions.
43 changes: 0 additions & 43 deletions ckanext/resourceproxy/tests/file_server.py

This file was deleted.

87 changes: 51 additions & 36 deletions ckanext/resourceproxy/tests/test_proxy.py
@@ -1,5 +1,7 @@
import requests
import unittest
import json
import httpretty

import paste.fixture
from pylons import config
Expand All @@ -10,12 +12,39 @@
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


class TestProxyBasic(tests.WsgiAppCase, unittest.TestCase):
JSON_STRING = json.dumps({
"a": "foo",
"bar": "yes, I'm proxied",
"b": 42})


def set_resource_url(url):
testpackage = model.Package.get('annakarenina')

context = {
'model': model,
'session': model.Session,
'user': model.User.get('testsysadmin').name
}

resource = logic.get_action('resource_show')(context, {'id': testpackage.resources[0].id})
package = logic.get_action('package_show')(context, {'id': testpackage.id})

resource['url'] = url
logic.action.update.resource_update(context, resource)

testpackage = model.Package.get('annakarenina')
assert testpackage.resources[0].url == resource['url'], testpackage.resources[0].url

return {'resource': resource, 'package': package}


class TestProxyPrettyfied(tests.WsgiAppCase, unittest.TestCase):

serving = False

Expand All @@ -26,12 +55,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 All @@ -42,41 +65,29 @@ def teardown_class(cls):
model.repo.rebuild_db()
plugins.reset()

def set_resource_url(self, url):
testpackage = model.Package.get('annakarenina')

context = {
'model': model,
'session': model.Session,
'user': model.User.get('testsysadmin').name
}

resource = logic.get_action('resource_show')(context, {'id': testpackage.resources[0].id})
package = logic.get_action('package_show')(context, {'id': testpackage.id})

resource['url'] = url
logic.action.update.resource_update(context, resource)

testpackage = model.Package.get('annakarenina')
assert testpackage.resources[0].url == resource['url'], testpackage.resources[0].url

self.data_dict = {'resource': resource, 'package': package}
def setUp(self):
self.url = 'http://www.ckan.org/static/example.json'
self.data_dict = set_resource_url(self.url)

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

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

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

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

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

proxied_url = proxy.get_proxified_resource_url(self.data_dict)
result = self.app.get(proxied_url, status='*')
assert result.status == 500, result.status
assert 'too large' in result.body, result.body

def test_resource_proxy_non_existent(self):
self.set_resource_url('http://foo.bar')
self.data_dict = set_resource_url('http://foo.bar')

def f1():
url = self.data_dict['resource']['url']
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 4bf6bcd

Please sign in to comment.