Skip to content

Commit

Permalink
Fixes jqb#1 by adding Content-Type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
romanbarczynski committed Nov 3, 2014
1 parent 6cc7d2b commit 14f07c7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion nbp/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ def download(url):
try:
resp = urllib2.urlopen(url)
if hasattr(resp, 'getcode') and resp.getcode() == 200:
return resp
if hasattr(resp, 'info') and resp.info()['content-type'] == 'text/xml':
return resp
return None
except urllib2.URLError, e:
resp = None
return resp
Expand Down
16 changes: 16 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import nbp
from datetime import date
from nose.tools import assert_equals


Expand All @@ -18,3 +19,18 @@ def test_downloads_exchange_rate_properly():
}
currency_data = nbp.download_exchange_rate(nbp.date(2010, 7, 11), 'EUR')
assert_equals(expexted, currency_data)

def test_downloads_exchange_rate_properly_even_with_broken_nbp_404_200():
expexted = {
'search_date': '2014-11-03',
'table_no': u'213/A/NBP/2014',
'pub_date': u'2014-11-03',
'url': 'http://rss.nbp.pl/kursy/xml2/2014/a/14a213.xml',
'currency': {
'name': u'euro',
'rate': 4.2209,
'code': u'EUR',
},
}
currency_data = nbp.download_exchange_rate(date.today(), 'EUR')
assert_equals(date.today().strftime('%Y-%m-%d'), currency_data['pub_date'])

1 comment on commit 14f07c7

@romanbarczynski
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just noticed that test needs tuning. will work on this tonight

Please sign in to comment.