"Fossies" - the Fresh Open Source Software Archive

Member "http-prompt-2.1.0/tests/context/test_context.py" (5 Mar 2021, 5024 Bytes) of package /linux/www/http-prompt-2.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_context.py": 1.0.0_vs_2.1.0.

    1 from http_prompt.context import Context
    2 
    3 
    4 def test_creation():
    5     context = Context('http://example.com')
    6     assert context.url == 'http://example.com'
    7     assert context.options == {}
    8     assert context.headers == {}
    9     assert context.querystring_params == {}
   10     assert context.body_params == {}
   11     assert not context.should_exit
   12 
   13 
   14 def test_creation_with_longer_url():
   15     context = Context('http://example.com/a/b/c/index.html')
   16     assert context.url == 'http://example.com/a/b/c/index.html'
   17     assert context.options == {}
   18     assert context.headers == {}
   19     assert context.querystring_params == {}
   20     assert context.body_params == {}
   21     assert not context.should_exit
   22 
   23 
   24 def test_eq():
   25     c1 = Context('http://localhost')
   26     c2 = Context('http://localhost')
   27     assert c1 == c2
   28 
   29     c1.options['--verify'] = 'no'
   30     assert c1 != c2
   31 
   32 
   33 def test_copy():
   34     c1 = Context('http://localhost')
   35     c2 = c1.copy()
   36     assert c1 == c2
   37     assert c1 is not c2
   38 
   39 
   40 def test_update():
   41     c1 = Context('http://localhost')
   42     c1.headers['Accept'] = 'application/json'
   43     c1.querystring_params['flag'] = '1'
   44     c1.body_params.update({
   45         'name': 'John Doe',
   46         'email': 'john@example.com'
   47     })
   48 
   49     c2 = Context('http://example.com')
   50     c2.headers['Content-Type'] = 'text/html'
   51     c2.body_params['name'] = 'John Smith'
   52 
   53     c1.update(c2)
   54 
   55     assert c1.url == 'http://example.com'
   56     assert c1.headers == {
   57         'Accept': 'application/json',
   58         'Content-Type': 'text/html'
   59     }
   60     assert c1.querystring_params == {'flag': '1'}
   61     assert c1.body_params == {
   62         'name': 'John Smith',
   63         'email': 'john@example.com'
   64     }
   65 
   66 
   67 def test_spec():
   68     c = Context('http://localhost', spec={
   69         'paths': {
   70             '/users': {
   71                 'get': {
   72                     'parameters': [
   73                         {'name': 'username', 'in': 'path'},
   74                         {'name': 'since', 'in': 'query'},
   75                         {'name': 'Accept'}
   76                     ]
   77                 }
   78             },
   79             '/orgs/{org}': {
   80                 'get': {
   81                     'parameters': [
   82                         {'name': 'org', 'in': 'path'},
   83                         {'name': 'featured', 'in': 'query'},
   84                         {'name': 'X-Foo', 'in': 'header'}
   85                     ]
   86                 }
   87             }
   88         }
   89     })
   90     assert c.url == 'http://localhost'
   91 
   92     root_children = list(sorted(c.root.children))
   93     assert len(root_children) == 2
   94     assert root_children[0].name == 'orgs'
   95     assert root_children[1].name == 'users'
   96 
   97     orgs_children = list(sorted(root_children[0].children))
   98     assert len(orgs_children) == 1
   99 
  100     org_children = list(sorted(list(orgs_children)[0].children))
  101     assert len(org_children) == 2
  102     assert org_children[0].name == 'X-Foo'
  103     assert org_children[1].name == 'featured'
  104 
  105     users_children = list(sorted(root_children[1].children))
  106     assert len(users_children) == 2
  107     assert users_children[0].name == 'Accept'
  108     assert users_children[1].name == 'since'
  109 
  110 
  111 def test_override():
  112     """Parameters can be defined at path level
  113     """
  114     c = Context('http://localhost', spec={
  115         'paths': {
  116             '/users': {
  117                 'parameters': [
  118                     {'name': 'username', 'in': 'query'},
  119                     {'name': 'Accept', 'in': 'header'}
  120                 ],
  121                 'get': {
  122                     'parameters': [
  123                         {'name': 'custom1', 'in': 'query'}
  124                     ]
  125                 },
  126                 'post': {
  127                     'parameters': [
  128                         {'name': 'custom2', 'in': 'query'},
  129                     ]
  130                 },
  131             },
  132             '/orgs': {
  133                 'parameters': [
  134                     {'name': 'username', 'in': 'query'},
  135                     {'name': 'Accept', 'in': 'header'}
  136                 ],
  137                 'get': {}
  138             }
  139         }
  140     })
  141     assert c.url == 'http://localhost'
  142 
  143     root_children = list(sorted(c.root.children))
  144     # one path
  145     assert len(root_children) == 2
  146     assert root_children[0].name == 'orgs'
  147     assert root_children[1].name == 'users'
  148 
  149     orgs_methods = list(sorted(list(root_children)[0].children))
  150     # path parameters are used even if no method parameter
  151     assert len(orgs_methods) == 2
  152     assert next(filter(lambda i:i.name == 'username', orgs_methods), None) is not None
  153     assert next(filter(lambda i:i.name == 'Accept', orgs_methods), None) is not None
  154 
  155     users_methods = list(sorted(list(root_children)[1].children))
  156     # path and methods parameters are merged
  157     assert len(users_methods) == 4
  158     assert next(filter(lambda i:i.name == 'username', users_methods), None) is not None
  159     assert next(filter(lambda i:i.name == 'custom1', users_methods), None) is not None
  160     assert next(filter(lambda i:i.name == 'custom2', users_methods), None) is not None
  161     assert next(filter(lambda i:i.name == 'Accept', users_methods), None) is not None