Example #1
0
def test_content_length():
    r0 = Response('x' * 10, content_length=10)

    req_head = Request.blank('/', method='HEAD')
    r1 = req_head.get_response(r0)
    assert r1.status_code == 200
    assert r1.body == b''
    assert r1.content_length == 10

    req_get = Request.blank('/')
    r2 = req_get.get_response(r0)
    assert r2.status_code == 200
    assert r2.body == b'x' * 10
    assert r2.content_length == 10

    r3 = Response(app_iter=[b'x'] * 10)
    assert r3.content_length is None
    assert r3.body == b'x' * 10
    assert r3.content_length == 10

    r4 = Response(app_iter=[b'x'] * 10,
                  content_length=20) # wrong content_length
    assert r4.content_length == 20
    with pytest.raises(AssertionError):
        r4.body

    req_range = Request.blank('/', range=(0, 5))
    r0.conditional_response = True
    r5 = req_range.get_response(r0)
    assert r5.status_code == 206
    assert r5.body == b'xxxxx'
    assert r5.content_length == 5
Example #2
0
def create_request_from_session(url, session, timeout=DEFAULT_TIMEOUT,
                                verify=True):
    try:
        # Use session to follow redirects:
        with closing(session.head(url, allow_redirects=True,
                                  timeout=timeout,
                                  verify=verify)) as head:
            req = Request.blank(head.url)
            req.environ['webob.client.timeout'] = timeout

            # Get cookies from head:
            cookies_dict = head.cookies.get_dict()

            # Set request cookies to the head cookies:
            req.headers['Cookie'] = ','.join(name + '=' +
                                             cookies_dict[name]
                                             for name in cookies_dict)
            # Set the headers to the session headers:
            for item in head.request.headers:
                req.headers[item] = head.request.headers[item]
            return req
    except (MissingSchema, InvalidSchema):
        # Missing schema can occur in tests when the url
        # is not pointing to any resource. Simply pass.
        req = Request.blank(url)
        req.environ['webob.client.timeout'] = timeout
        return req
    except Timeout:
        raise HTTPError('Timeout')
Example #3
0
def test_content_length():
    r0 = Response('x'*10, content_length=10)

    req_head = Request.blank('/', method='HEAD')
    r1 = req_head.get_response(r0)
    eq_(r1.status_code, 200)
    eq_(r1.body, b'')
    eq_(r1.content_length, 10)

    req_get = Request.blank('/')
    r2 = req_get.get_response(r0)
    eq_(r2.status_code, 200)
    eq_(r2.body, b'x'*10)
    eq_(r2.content_length, 10)

    r3 = Response(app_iter=[b'x']*10)
    eq_(r3.content_length, None)
    eq_(r3.body, b'x'*10)
    eq_(r3.content_length, 10)

    r4 = Response(app_iter=[b'x']*10,
                  content_length=20) # wrong content_length
    eq_(r4.content_length, 20)
    assert_raises(AssertionError, lambda: r4.body)

    req_range = Request.blank('/', range=(0,5))
    r0.conditional_response = True
    r5 = req_range.get_response(r0)
    eq_(r5.status_code, 206)
    eq_(r5.body, b'xxxxx')
    eq_(r5.content_length, 5)
Example #4
0
def test_get_extension_bad():
    params = {'data-format': 'unsupported_extension'}
    req = Request.blank('?' + urlencode(params))
    assert get_extension(req.environ) == None

    # data-format not in the request params
    req = Request.blank('')
    assert get_extension(req.environ) == None
Example #5
0
    def tapp(env, sr):
        req = Request(env)
        req = req.decode()

        v = req.POST[req.query_string]

        if hasattr(v, 'filename'):
            r = Response(text_('%s\n%r' % (v.filename, v.value)))
        else:
            r = Response(v)
        return r(env, sr)
Example #6
0
def test_conditional_response_if_none_match_weak():
    req = Request.blank('/', headers={'if-none-match': '"bar"'})
    req_weak = Request.blank('/', headers={'if-none-match': 'W/"bar"'})
    resp = Response(app_iter=['foo\n'], conditional_response=True, etag='bar')
    resp_weak = Response(app_iter=['foo\n'], conditional_response=True, headers={'etag': 'W/"bar"'})
    for rq in [req, req_weak]:
        for rp in [resp, resp_weak]:
            rq.get_response(rp).status_code == 304

    r2 = Response(app_iter=['foo\n'], conditional_response=True, headers={'etag': '"foo"'})
    r2_weak = Response(app_iter=['foo\n'], conditional_response=True, headers={'etag': 'W/"foo"'})
    req_weak.get_response(r2).status_code == 200
    req.get_response(r2_weak) == 200
 def tapp(env, sr):
     req = Request(env)
     #import pprint; pprint.pprint(req.environ)
     #print(req.body)
     req = req.decode()
     #import pprint; pprint.pprint(req.environ)
     #print(req.body)
     v = req.POST[req.query_string]
     if hasattr(v, 'filename'):
         r = Response(text_('%s\n%r' % (v.filename, v.value)))
     else:
         r = Response(v)
     return r(env, sr)
Example #8
0
def test_environ_getter_default_fset():
    from webob.descriptors import environ_getter

    req = Request.blank("/")
    desc = environ_getter("akey", default="the_default")
    desc.fset(req, "bar")
    eq_(req.environ["akey"], "bar")
def test_multi_layer_dataset(multi_layer_app, temp_file):
    req = Request.blank("/")
    resp = req.get_response(multi_layer_app)
    assert resp.status == "200 OK"

    for chunk in resp.app_iter:
        temp_file.write(chunk)
    temp_file.flush()

    z = ZipFile(temp_file.name, "r", ZIP_DEFLATED)
    assert z

    # Should be 2 files for each layer
    assert len(z.namelist()) == 2 * 4
    assert "my_grid_0.asc" in z.namelist()

    # find the first asc file
    asc_filename = filter(lambda x: x.endswith(".asc"), z.namelist())[0]

    with z.open(asc_filename, "r") as f:
        data = f.read()

    assert (
        data
        == """ncols        3
nrows        2
xllcorner    -122.500000000000
yllcorner    53.000000000000
dx           -0.500000000000
dy           1.000000000000
NODATA_value  -9999
 0 1 2
 3 4 5
"""
    )
def test_no_duplicate_headers(real_data_test):
    req = Request.blank(
        "/pr+tasmax+tasmin_day_BCCA+ANUSPLIN300+ACCESS1-0_historical+rcp45_r1i1p1_19500101-21001231.h5.aig?tasmax&"
    )
    resp = req.get_response(real_data_test)
    assert resp.status == "200 OK"
    assert len(resp.headers.keys()) == len(set(resp.headers.keys()))
Example #11
0
def test_HEAD_conditional_response_range_empty_response():
    req = Request.blank('/',
        method = 'HEAD',
        range=(4,5),
    )
    res = Response('Are we not men?', conditional_response=True)
    assert req.get_response(res).body == b''
Example #12
0
def test_environ_getter_default_fset_none():
    from webob.descriptors import environ_getter
    req = Request.blank('/')
    desc = environ_getter('akey', default='the_default')
    desc.fset(req, 'baz')
    desc.fset(req, None)
    assert 'akey' not in req.environ
Example #13
0
def test_accept_property_fdel():
    desc = accept_property('Accept-Charset', '14.2')
    req = Request.blank('/', environ={'envkey': 'envval'})
    desc.fset(req, 'val')
    assert desc.fget(req).header_value == 'val'
    desc.fdel(req)
    eq_(type(desc.fget(req)), NilAccept)
Example #14
0
def test_environ_getter_nodefault_fget():
    from webob.descriptors import environ_getter

    req = Request.blank("/")
    desc = environ_getter("akey")
    desc.fset(req, "bar")
    assert req.environ["akey"] == "bar"
Example #15
0
def test_disconnect_detection_hinted_readline():
    data = 'abc'*(1<<20)
    req = Request.blank('/', POST=data)
    req.is_body_seekable = False
    line = req.body_file.readline(1<<16)
    assert line
    assert bytes_(data).startswith(line)
Example #16
0
    def test_middleware_direct_call(self):
        @wsgify.middleware
        def mw(req, app):
            return 'foo'

        app = mw(Response())
        self.assertEqual(app(Request.blank('/')), 'foo')
Example #17
0
def test_operational_error():
    app = ErrorMiddleware(myapp)
    req = Request.blank('/')
    resp = req.get_response(app, catch_exc_info=True)
    assert resp.status_code == 503
    assert 'Retry-After' in resp.headers
    assert 'accessing the database' in resp.body
Example #18
0
def test_HEAD_conditional_response_returns_empty_response():
    req = Request.blank('/', method='HEAD', if_none_match='none')
    res = Response(conditional_response=True)
    def start_response(status, headerlist):
        pass
    result = res(req.environ, start_response)
    assert not list(result)
Example #19
0
def test_conditional_response_if_modified_since_true():
    from datetime import datetime, timedelta
    req = Request.blank('/', if_modified_since=datetime(2011, 3, 17, 13, 0, 0))
    resp = Response(app_iter=['foo\n'], conditional_response=True,
                    last_modified=req.if_modified_since + timedelta(seconds=1))
    resp = req.get_response(resp)
    assert resp.status_code == 200
Example #20
0
def test_aaigrid_response(pcic_data_portal, url):
    base = '/data/downscaled_gcms_archive/pr+tasmax+tasmin_day_'
    req = Request.blank(url.format(base))
    resp = req.get_response(pcic_data_portal)

    assert resp.status == '200 OK'
    assert resp.content_type == 'application/zip'
Example #21
0
def test_upath_property_fset():
    from webob.descriptors import upath_property

    req = Request.blank("/")
    desc = upath_property("akey")
    desc.fset(req, "avalue")
    eq_(desc.fget(req), "avalue")
Example #22
0
def test_environ_getter_nodefault_keyerror():
    from webob.descriptors import environ_getter

    req = Request.blank("/")
    desc = environ_getter("akey")
    with pytest.raises(KeyError):
        desc.fget(req)
Example #23
0
    def test_middleware_direct_call(self):
        @wsgify.middleware
        def mw(req, app):
            return "foo"

        app = mw(Response())
        self.assertEqual(app(Request.blank("/")), "foo")
Example #24
0
    def test(post):
        req = Request.blank("/?a", POST=post)
        req.environ[
            "CONTENT_TYPE"
        ] = "multipart/form-data; charset=windows-1251; boundary=BOUNDARY"

        return req.get_response(tapp)
Example #25
0
def test_climatology_bounds(pcic_data_portal):
    url = '/data/bc_prism/tmin_monClim_PRISM_historical_run1_197101-200012'\
          '.nc.nc?climatology_bounds&'
    req = Request.blank(url)
    resp = req.get_response(pcic_data_portal)

    assert resp.status == '200 OK'
    assert resp.content_type == 'application/x-netcdf'

    f = NamedTemporaryFile(suffix='.nc', delete=False)
    for block in resp.app_iter:
        f.write(block)
    f.close()

    nc = netCDF4.Dataset(f.name)

    assert 'climatology_bounds' in nc.variables

    assert_almost_equal(nc.variables['climatology_bounds'][:],
                        np.array([[0.,  10988.],
                                  [31.,  11017.],
                                  [59.,  11048.],
                                  [90.,  11078.],
                                  [120.,  11109.],
                                  [151.,  11139.],
                                  [181.,  11170.],
                                  [212.,  11201.],
                                  [243.,  11231.],
                                  [273.,  11262.],
                                  [304.,  11292.],
                                  [334.,  11323.],
                                  [0.,  11323.]], dtype=np.float32))
    nc.close()
    os.remove(f.name)
Example #26
0
def test_accept_property_fdel():
    desc = accept_property("Accept-Charset", "14.2")
    req = Request.blank("/", environ={"envkey": "envval"})
    desc.fset(req, "val")
    assert desc.fget(req).header_value == "val"
    desc.fdel(req)
    eq_(type(desc.fget(req)), NilAccept)
Example #27
0
def test_prism_response(pcic_data_portal):
    req = Request.blank(
        '/data/bc_prism/tmin_monClim_PRISM_historical_run1_197101-200012'
        '.nc.html')
    resp = req.get_response(pcic_data_portal)
    assert resp.status == '200 OK'
    assert resp.content_type == 'text/html'
Example #28
0
def test_clip_to_date_one(pcic_data_portal):
    base_url = '/data/pcds/agg/?'
    sdate = datetime(2007, 01, 01)
    params = {'from-date': sdate.strftime('%Y/%m/%d'),
              'network-name': 'RTA', 'data-format': 'csv',
              'cliptodate': 'cliptodate',
              }
    req = Request.blank(base_url + urlencode(params))

    resp = req.get_response(pcic_data_portal)
    print resp.status
    assert resp.status == '200 OK'
    t = TemporaryFile()
    t.write(resp.body)
    z = ZipFile(t, 'r')
    assert 'RTA/pondosy.csv' in z.namelist()
    f = z.open("RTA/pondosy.csv")
    [f.readline() for _ in range(10)]
    # Read through the file and ensure the no data outside of the date
    # range was returned
    reader = csv.reader(f)
    for row in reader:
        if len(row) > 0:
            d = datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S')
            assert d >= sdate
    # Check values on the first 5 just to make sure
    expected = ['2007-01-09 00:00:00',
                '2007-01-10 00:00:00',
                '2007-01-11 00:00:00',
                '2007-01-12 00:00:00',
                '2007-01-13 00:00:00']
    for exp, actual in izip(expected, reader):
        assert exp[0] == actual
Example #29
0
def test_nc_response(pcic_data_portal):
    url = '/data/pcds/lister/climo/EC/1010066.csql.nc?'
    'station_observations.Precip_Climatology,station_observations.time'
    req = Request.blank(url)
    resp = req.get_response(pcic_data_portal)
    assert resp.status == '200 OK'
    assert resp.content_type == 'application/x-netcdf'

    f = NamedTemporaryFile(suffix='.nc', delete=False)
    for block in resp.app_iter:
        f.write(block)
    f.close()

    nc = netCDF4.Dataset(f.name)
    assert_almost_equal(nc.longitude, np.array(-123.28333), 4)
    assert nc.station_id == '1010066'
    assert nc.station_name == 'ACTIVE PASS'  # station name
    var = nc.variables['Precip_Climatology']
    assert var.cell_method == 't: sum within months t: mean over years'
    prcp = [127.128, 91.2249, 77.3313, 46.2816, 39.6803, 33.1902, 22.3557,
            22.448, 38.5025, 72.3281, 144.159, 121.008]
    for actual, expected in zip(var, prcp):
        assert actual == expected
    assert nc.variables['time'].type == 'Float64'
    nc.close()
    os.remove(f.name)
Example #30
0
 def test_bad_grade_decimal(self):
     """
     Grade returned from Tool Provider doesn't use a period as the decimal point.
     """
     self.xmodule.verify_oauth_body_sign = Mock()
     request = Request(self.environ)
     request.body = self.get_request_body(params={'grade': '0,5'})
     response = self.xmodule.grade_handler(request, '')
     real_response = self.get_response_values(response)
     expected_response = {
         'action': None,
         'code_major': 'failure',
         'description': 'Request body XML parsing error: invalid literal for float(): 0,5',
         'messageIdentifier': 'unknown',
     }
     self.assertEqual(response.status_code, 200)
     self.assertDictEqual(expected_response, real_response)
Example #31
0
def test_environ_getter_nodefault_keyerror():
    from webob.descriptors import environ_getter
    req = Request.blank('/')
    desc = environ_getter('akey')
    assert_raises(KeyError, desc.fget, req)
Example #32
0
def test_request_readlines():
    req = Request.blank('/', POST='a\n' * 3)
    req.is_body_seekable = False
    eq(req.body_file.readlines(), [b'a\n'] * 3)
Example #33
0
def test_json_body_valid_json():
    request = Request.blank('/', POST=b'{"a":1}')
    eq(request.json_body, {'a': 1})
Example #34
0
def test_hydro_model_archive_5var(pcic_data_portal, url):
    base = '/data/hydro_model_archive/5var_day_'
    req = Request.blank(url.format(base))
    resp = req.get_response(pcic_data_portal)
    assert resp.status == '200 OK'
    assert resp.content_type == 'application/x-netcdf'
Example #35
0
def test_request_delete_with_body():
    req = Request.blank('/', method='DELETE')
    assert not req.is_body_readable
    req.body = b'abc'
    assert req.is_body_readable
    assert req.body_file.read() == b'abc'
Example #36
0
def test_conditional_response_if_none_match_false():
    req = Request.blank('/', if_none_match='foo')
    resp = Response(app_iter=['foo\n'], conditional_response=True, etag='bar')
    resp = req.get_response(resp)
    assert resp.status_code == 200
Example #37
0
def get_response(app, path='/', **req_kw):
    """Convenient function to query an application"""
    req = Request(environ_from_url(path), **req_kw)
    return req.get_response(app)
Example #38
0
def test_static(url):
    static_app = static.Cling(resource_filename('pdp', 'static'))
    req = Request.blank(url)
    resp = req.get_response(static_app)
    assert resp.status == '200 OK'
Example #39
0
def test_record_length(filters, pcic_data_portal):
    req = Request.blank('/pcds/count_stations?' + urlencode(filters))
    resp = req.get_response(pcic_data_portal)
    assert resp.status == '200 OK'
    assert resp.content_type == 'application/json'
    assert 'stations_selected' in resp.body
Example #40
0
def test_conditional_response_if_none_match_true():
    req = Request.blank('/', if_none_match='foo')
    resp = Response(app_iter=['foo\n'], conditional_response=True, etag='foo')
    resp = req.get_response(resp)
    eq_(resp.status_int, 304)
Example #41
0
def test_nc_response_with_null_values(pcic_data_portal):
    req = Request.blank('/data/pcds/lister/raw/BCH/AKI.rsql.nc')
    resp = req.get_response(pcic_data_portal)
    assert resp.status == '200 OK'
    assert resp.content_type == 'application/x-netcdf'
Example #42
0
def test_http_only_cookie():
    req = Request.blank('/')
    res = req.get_response(Response('blah'))
    res.set_cookie("foo", "foo", httponly=True)
    eq_(res.headers['set-cookie'], 'foo=foo; Path=/; HttpOnly')
Example #43
0
def test_environ_getter_default_fset():
    from webob.descriptors import environ_getter
    req = Request.blank('/')
    desc = environ_getter('akey', default='the_default')
    desc.fset(req, 'bar')
    eq_(req.environ['akey'], 'bar')
Example #44
0
def test_environ_getter_default_fget():
    from webob.descriptors import environ_getter
    req = Request.blank('/')
    desc = environ_getter('akey', default='the_default')
    eq_(desc.fget(req), 'the_default')
Example #45
0
def test_upath_property_fget():
    from webob.descriptors import upath_property

    req = Request.blank("/")
    desc = upath_property("akey")
    assert desc.fget(req) == ""
Example #46
0
def test_input_polygon_download_zipfile(pcic_data_portal, polygon):
    polygons = {
        'single station polygon':
        'MULTIPOLYGON(('
        '(-128.59910830928055 53.852860003953495,'
        '-128.45182033352623 53.86465328654807,'
        '-128.4562364026459 53.79618867900996,'
        '-128.59910830928055 53.852860003953495)))',
        'multiple station polygon':
        'MULTIPOLYGON(('
        '(-122.16988794027921 54.61618496834933,'
        '-122.12395804699314 54.61753974917094,'
        '-122.12601061930596 54.59023544661601,'
        '-122.16988794027921 54.61618496834933)))',
        'multiple polygons':
        'MULTIPOLYGON(('
        '(-121.89643424564886 54.20043741333826,'
        '-122.13176870283458 54.14845194258505,'
        '-122.1226369471675 54.01876407450885,'
        '-121.75836848952896 54.048830061202494,'
        '-121.89643424564886 54.20043741333826)),'
        '((-121.23964475389124 54.0095741960868,'
        '-121.45248834584457 53.967985385684926,'
        '-121.43980080067858 53.81312824978667,'
        '-121.108695973217 53.82251331340121,'
        '-121.23964475389124 54.0095741960868)))',
        'station EC 1025230':
        'MULTIPOLYGON(('
        '(-125.29637443283997 49.75425334879682,'
        '-125.27276223016982 49.757216316386696,'
        '-125.27059060328506 49.742548670551834,'
        '-125.29276284376195 49.73895329426124,'
        '-125.29637443283997 49.75425334879682)))',
        'station without observations polygon':
        'MULTIPOLYGON(('
        '(-123.28294974546985 48.53765964184428,'
        '-123.22831848366447 48.573754711908165,'
        '-123.20548795617668 48.51564288151938,'
        '-123.28294974546985 48.53765964184428)))',
        'network without observations polygon':
        'MULTIPOLYGON(('
        '(-122.84250052310462 49.33095465473638,'
        '-122.90005834526038 49.297649140301004,'
        '-122.86157366135 49.24657317992617,'
        '-122.76249620948452 49.219908575081675,'
        '-122.66148902126935 49.24806074593272,'
        '-122.70891368322285 49.30448396541877,'
        '-122.84250052310462 49.33095465473638)))'
    }

    base_url = '/data/pcds/agg/?'
    params = {
        'from-date': 'YYYY/MM/DD',
        'to-date': 'YYYY/MM/DD',
        'input-polygon': polygons[polygon],
        'data-format': 'ascii',
        'download-timeseries': 'Timeseries'
    }

    req = Request.blank(base_url + urlencode(params))

    resp = req.get_response(pcic_data_portal)
    assert resp.status == '200 OK'

    t = TemporaryFile()
    t.write(resp.body)
    z = ZipFile(t, 'r')
    assert z.testzip() is None
Example #47
0
 def _testit(self, app, req):
     if isinstance(req, str):
         req = Request.blank(req)
     resp = req.get_response(app)
     return resp
Example #48
0
def test_no_404s(pcic_data_portal, url):
    req = Request.blank(url)
    resp = req.get_response(pcic_data_portal)
    assert resp.status == '200 OK'
Example #49
0
def test_HEAD_conditional_response_range_empty_response():
    req = Request.blank('/', method='HEAD', range=(4, 5))
    res = Response('Are we not men?', conditional_response=True)
    assert req.get_response(res).body == b''
Example #50
0
def test_hydro_model_out_pr_tasmin_tasmax_wind(pcic_data_portal, url):
    base = '/data/hydro_model_out/pr+tasmin+tasmax+wind_day_'
    req = Request.blank(url.format(base))
    resp = req.get_response(pcic_data_portal)
    assert resp.status == '200 OK'
    assert resp.content_type == 'application/x-netcdf'
Example #51
0
def test_unsupported_extension(pcic_data_portal):
    req = Request.blank('/data/pcds/agg/?data-format=foo')
    req.method = 'POST'
    resp = req.get_response(pcic_data_portal)
    assert resp.status == '400 Bad Request'
def test_upath_property_fget():
    from webob.descriptors import upath_property
    req = Request.blank('/')
    desc = upath_property('akey')
    assert desc.fget(req) == ''
Example #53
0
def test_limited_length_file_repr():
    req = Request.blank('/', POST='x')
    req.body_file_raw = 'dummy'
    req.is_body_seekable = False
    eq(repr(req.body_file.raw), "<LimitedLengthFile('dummy', maxlen=1)>")
def test_environ_getter_nodefault_fget():
    from webob.descriptors import environ_getter
    req = Request.blank('/')
    desc = environ_getter('akey')
    desc.fset(req, 'bar')
    assert req.environ['akey'] == 'bar'
Example #55
0
def test_request_no_method():
    assert Request({}).method == 'GET'
def test_environ_getter_nodefault_keyerror():
    from webob.descriptors import environ_getter
    req = Request.blank('/')
    desc = environ_getter('akey')
    with pytest.raises(KeyError):
        desc.fget(req)
Example #57
0
def test_json_body_GET_request():
    request = Request.blank('/')
    assert_raises(ValueError, getattr, request, 'json_body')
Example #58
0
def test_environ_getter_default_fget():
    from webob.descriptors import environ_getter

    req = Request.blank("/")
    desc = environ_getter("akey", default="the_default")
    assert desc.fget(req) == "the_default"
Example #59
0
def test_upath_property_fset():
    from webob.descriptors import upath_property
    req = Request.blank('/')
    desc = upath_property('akey')
    desc.fset(req, 'avalue')
    eq_(desc.fget(req), 'avalue')
Example #60
0
def test_exception_with_unicode_data():
    req = Request.blank("/", method="POST")
    res = req.get_response(method_not_allowed_app)
    assert res.status_code == 405