"Fossies" - the Fresh Open Source Software Archive

Member "http-prompt-2.1.0/tests/test_lexer.py" (5 Mar 2021, 25723 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.

    1 import unittest
    2 
    3 from pygments.token import Keyword, String, Text, Error, Name, Operator
    4 
    5 from http_prompt.lexer import HttpPromptLexer
    6 
    7 
    8 class LexerTestCase(unittest.TestCase):
    9 
   10     def setUp(self):
   11         self.lexer = HttpPromptLexer()
   12 
   13     def get_tokens(self, text, filter_spaces=True):
   14         tokens = self.lexer.get_tokens(text)
   15         tokens = filter(lambda t: t[1], tokens)
   16         if filter_spaces:
   17             tokens = filter(lambda t: t[1].strip(), tokens)
   18         return list(tokens)
   19 
   20 
   21 class TestLexer_mutation(LexerTestCase):
   22 
   23     def test_querystring(self):
   24         self.assertEqual(self.get_tokens('foo==bar'), [
   25             (Name, 'foo'),
   26             (Operator, '=='),
   27             (String, 'bar')
   28         ])
   29 
   30     def test_body_param(self):
   31         self.assertEqual(self.get_tokens('foo=bar'), [
   32             (Name, 'foo'),
   33             (Operator, '='),
   34             (String, 'bar')
   35         ])
   36 
   37     def test_header(self):
   38         self.assertEqual(self.get_tokens('Accept:application/json'), [
   39             (Name, 'Accept'),
   40             (Operator, ':'),
   41             (String, 'application/json')
   42         ])
   43 
   44     def test_json_integer(self):
   45         self.assertEqual(self.get_tokens('number:=1'), [
   46             (Name, 'number'),
   47             (Operator, ':='),
   48             (String, '1')
   49         ])
   50 
   51     def test_json_boolean(self):
   52         self.assertEqual(self.get_tokens('enabled:=true'), [
   53             (Name, 'enabled'),
   54             (Operator, ':='),
   55             (String, 'true')
   56         ])
   57 
   58     def test_json_string(self):
   59         self.assertEqual(self.get_tokens('name:="foo bar"'), [
   60             (Name, 'name'),
   61             (Operator, ':='),
   62             (Text, '"'),
   63             (String, 'foo bar'),
   64             (Text, '"')
   65         ])
   66 
   67     def test_json_array(self):
   68         self.assertEqual(self.get_tokens('list:=[1,"two"]'), [
   69             (Name, 'list'),
   70             (Operator, ':='),
   71             (String, '[1,"two"]'),
   72         ])
   73 
   74     def test_json_array_quoted(self):
   75         self.assertEqual(self.get_tokens("""list:='[1,"two"]'"""), [
   76             (Name, 'list'),
   77             (Operator, ':='),
   78             (Text, "'"),
   79             (String, '[1,"two"]'),
   80             (Text, "'"),
   81         ])
   82 
   83     def test_json_object(self):
   84         self.assertEqual(self.get_tokens('object:={"id":123,"name":"foo"}'), [
   85             (Name, 'object'),
   86             (Operator, ':='),
   87             (String, '{"id":123,"name":"foo"}'),
   88         ])
   89 
   90     def test_json_object_quoted(self):
   91         self.assertEqual(self.get_tokens("""object:='{"id": 123}'"""), [
   92             (Name, 'object'),
   93             (Operator, ':='),
   94             (Text, "'"),
   95             (String, '{"id": 123}'),
   96             (Text, "'")
   97         ])
   98 
   99     def test_json_escaped_colon(self):
  100         self.assertEqual(self.get_tokens(r'where[id\:gt]:=2'), [
  101             (Name, r'where[id\:gt]'),
  102             (Operator, ':='),
  103             (String, '2')
  104         ])
  105 
  106     def test_body_param_escaped_equal(self):
  107         self.assertEqual(self.get_tokens(r'foo\=bar=hello'), [
  108             (Name, r'foo\=bar'),
  109             (Operator, '='),
  110             (String, 'hello')
  111         ])
  112 
  113     def test_parameter_name_including_http_method_name(self):
  114         self.assertEqual(self.get_tokens('heading==hello'), [
  115             (Name, 'heading'),
  116             (Operator, '=='),
  117             (String, 'hello')
  118         ])
  119 
  120 
  121 class TestLexer_cd(LexerTestCase):
  122 
  123     def test_simple(self):
  124         self.assertEqual(self.get_tokens('cd api/v1'), [
  125             (Keyword, 'cd'),
  126             (String, 'api/v1')
  127         ])
  128 
  129     def test_double_quoted(self):
  130         self.assertEqual(self.get_tokens('cd "api/v 1"'), [
  131             (Keyword, 'cd'),
  132             (Text, '"'),
  133             (String, 'api/v 1'),
  134             (Text, '"')
  135         ])
  136 
  137     def test_single_quoted(self):
  138         self.assertEqual(self.get_tokens("cd 'api/v 1'"), [
  139             (Keyword, 'cd'),
  140             (Text, "'"),
  141             (String, 'api/v 1'),
  142             (Text, "'")
  143         ])
  144 
  145     def test_escape(self):
  146         self.assertEqual(self.get_tokens(r"cd api/v\ 1"), [
  147             (Keyword, 'cd'),
  148             (String, r'api/v\ 1')
  149         ])
  150 
  151     def test_second_path(self):
  152         self.assertEqual(self.get_tokens(r"cd api v1"), [
  153             (Keyword, 'cd'),
  154             (String, 'api'),
  155             (Error, 'v'),
  156             (Error, '1')
  157         ])
  158 
  159     def test_leading_trailing_spaces(self):
  160         self.assertEqual(self.get_tokens('   cd   api/v1  '), [
  161             (Keyword, 'cd'),
  162             (String, 'api/v1')
  163         ])
  164 
  165 
  166 class TestLexer_ls(LexerTestCase):
  167 
  168     def test_no_path(self):
  169         self.assertEqual(self.get_tokens('ls'), [
  170             (Keyword, 'ls')
  171         ])
  172 
  173     def test_path(self):
  174         self.assertEqual(self.get_tokens('ls api/v1'), [
  175             (Keyword, 'ls'),
  176             (String, 'api/v1')
  177         ])
  178 
  179     def test_second_path(self):
  180         self.assertEqual(self.get_tokens(r"ls api v1"), [
  181             (Keyword, 'ls'),
  182             (String, 'api'),
  183             (Error, 'v'),
  184             (Error, '1')
  185         ])
  186 
  187     def test_leading_trailing_spaces(self):
  188         self.assertEqual(self.get_tokens('   ls   api/v1  '), [
  189             (Keyword, 'ls'),
  190             (String, 'api/v1')
  191         ])
  192 
  193     def test_redirect(self):
  194         self.assertEqual(self.get_tokens('ls api/v1 > endpoints.txt'), [
  195             (Keyword, 'ls'),
  196             (String, 'api/v1'),
  197             (Operator, '>'),
  198             (String, 'endpoints.txt')
  199         ])
  200 
  201 
  202 class TestLexer_env(LexerTestCase):
  203 
  204     def test_env_simple(self):
  205         self.assertEqual(self.get_tokens('env'), [
  206             (Keyword, 'env'),
  207         ])
  208 
  209     def test_env_with_spaces(self):
  210         self.assertEqual(self.get_tokens('   env    '), [
  211             (Keyword, 'env'),
  212         ])
  213 
  214     def test_env_write(self):
  215         self.assertEqual(self.get_tokens('env > /tmp/file.txt'), [
  216             (Keyword, 'env'), (Operator, '>'),
  217             (String, '/tmp/file.txt')
  218         ])
  219 
  220     def test_env_append(self):
  221         self.assertEqual(self.get_tokens('env >> /tmp/file.txt'), [
  222             (Keyword, 'env'), (Operator, '>>'),
  223             (String, '/tmp/file.txt')
  224         ])
  225 
  226     def test_env_write_quoted_filename(self):
  227         self.assertEqual(self.get_tokens('env > "/tmp/my file.txt"'), [
  228             (Keyword, 'env'), (Operator, '>'),
  229             (Text, '"'), (String, '/tmp/my file.txt'), (Text, '"')
  230         ])
  231 
  232     def test_env_append_escaped_filename(self):
  233         self.assertEqual(self.get_tokens(r'env >> /tmp/my\ file.txt'), [
  234             (Keyword, 'env'), (Operator, '>>'),
  235             (String, r'/tmp/my\ file.txt')
  236         ])
  237 
  238     def test_env_pipe(self):
  239         self.assertEqual(self.get_tokens('env | grep name'), [
  240             (Keyword, 'env'), (Operator, '|'),
  241             (Text, 'grep'), (Text, 'name')
  242         ])
  243 
  244 
  245 class TestLexer_rm(LexerTestCase):
  246 
  247     def test_header(self):
  248         self.assertEqual(self.get_tokens('rm -h Accept'), [
  249             (Keyword, 'rm'),
  250             (Name, '-h'),
  251             (String, 'Accept')
  252         ])
  253 
  254     def test_header_escaped(self):
  255         self.assertEqual(self.get_tokens(r'rm -h Custom\ Header'), [
  256             (Keyword, 'rm'),
  257             (Name, '-h'),
  258             (String, r'Custom\ Header')
  259         ])
  260 
  261     def test_querystring(self):
  262         self.assertEqual(self.get_tokens('rm -q page'), [
  263             (Keyword, 'rm'),
  264             (Name, '-q'),
  265             (String, 'page')
  266         ])
  267 
  268     def test_querystring_double_quoted(self):
  269         self.assertEqual(self.get_tokens('rm -q "page size"'), [
  270             (Keyword, 'rm'),
  271             (Name, '-q'),
  272             (Text, '"'),
  273             (String, 'page size'),
  274             (Text, '"')
  275         ])
  276 
  277     def test_body_param(self):
  278         self.assertEqual(self.get_tokens('rm -b name'), [
  279             (Keyword, 'rm'),
  280             (Name, '-b'),
  281             (String, 'name')
  282         ])
  283 
  284     def test_body_param_single_quoted(self):
  285         self.assertEqual(self.get_tokens("rm -b 'first name'"), [
  286             (Keyword, 'rm'),
  287             (Name, '-b'),
  288             (Text, "'"),
  289             (String, 'first name'),
  290             (Text, "'")
  291         ])
  292 
  293     def test_option(self):
  294         self.assertEqual(self.get_tokens('rm -o --json'), [
  295             (Keyword, 'rm'),
  296             (Name, '-o'),
  297             (String, '--json')
  298         ])
  299 
  300     def test_reset(self):
  301         self.assertEqual(self.get_tokens('rm *'), [
  302             (Keyword, 'rm'),
  303             (Name, '*')
  304         ])
  305 
  306     def test_option_leading_trailing_spaces(self):
  307         self.assertEqual(self.get_tokens('  rm  -o    --json   '), [
  308             (Keyword, 'rm'),
  309             (Name, '-o'),
  310             (String, '--json')
  311         ])
  312 
  313     def test_invalid_type(self):
  314         self.assertEqual(self.get_tokens('rm -a foo'), [
  315             (Keyword, 'rm'),
  316             (Error, '-'), (Error, 'a'),
  317             (Error, 'f'), (Error, 'o'), (Error, 'o')
  318         ])
  319 
  320 
  321 class TestLexer_help(LexerTestCase):
  322 
  323     def test_help_simple(self):
  324         self.assertEqual(self.get_tokens('help'), [
  325             (Keyword, 'help')
  326         ])
  327 
  328     def test_help_with_spaces(self):
  329         self.assertEqual(self.get_tokens('  help   '), [
  330             (Keyword, 'help')
  331         ])
  332 
  333 
  334 class TestLexer_source(LexerTestCase):
  335 
  336     def test_source_simple_filename(self):
  337         self.assertEqual(self.get_tokens('source file.txt'), [
  338             (Keyword, 'source'), (String, 'file.txt')
  339         ])
  340 
  341     def test_source_with_spaces(self):
  342         self.assertEqual(self.get_tokens('  source    file.txt    '), [
  343             (Keyword, 'source'), (String, 'file.txt')
  344         ])
  345 
  346     def test_source_quoted_filename(self):
  347         self.assertEqual(self.get_tokens("source '/tmp/my file.txt'"), [
  348             (Keyword, 'source'),
  349             (Text, "'"), (String, '/tmp/my file.txt'), (Text, "'")
  350         ])
  351 
  352     def test_source_escaped_filename(self):
  353         self.assertEqual(self.get_tokens(r"source /tmp/my\ file.txt"), [
  354             (Keyword, 'source'), (String, r'/tmp/my\ file.txt')
  355         ])
  356 
  357 
  358 class TestLexer_exec(LexerTestCase):
  359 
  360     def test_exec_simple_filename(self):
  361         self.assertEqual(self.get_tokens('exec file.txt'), [
  362             (Keyword, 'exec'), (String, 'file.txt')
  363         ])
  364 
  365     def test_exec_with_spaces(self):
  366         self.assertEqual(self.get_tokens('  exec    file.txt    '), [
  367             (Keyword, 'exec'), (String, 'file.txt')
  368         ])
  369 
  370     def test_exec_quoted_filename(self):
  371         self.assertEqual(self.get_tokens("exec '/tmp/my file.txt'"), [
  372             (Keyword, 'exec'),
  373             (Text, "'"), (String, '/tmp/my file.txt'), (Text, "'")
  374         ])
  375 
  376     def test_exec_escaped_filename(self):
  377         self.assertEqual(self.get_tokens(r"exec /tmp/my\ file.txt"), [
  378             (Keyword, 'exec'), (String, r'/tmp/my\ file.txt')
  379         ])
  380 
  381 
  382 class TestLexer_exit(LexerTestCase):
  383 
  384     def test_exit_simple(self):
  385         self.assertEqual(self.get_tokens('exit'), [
  386             (Keyword, 'exit')
  387         ])
  388 
  389     def test_exit_with_spaces(self):
  390         self.assertEqual(self.get_tokens('  exit   '), [
  391             (Keyword, 'exit')
  392         ])
  393 
  394 
  395 class TestLexerPreview(LexerTestCase):
  396 
  397     def test_httpie_without_action(self):
  398         cmd = 'httpie http://example.com name=jack'
  399         self.assertEqual(self.get_tokens(cmd), [
  400             (Keyword, 'httpie'),
  401             (String, 'http://example.com'),
  402             (Name, 'name'), (Operator, '='), (String, 'jack')
  403         ])
  404 
  405     def test_httpie_without_action_and_url(self):
  406         cmd = 'httpie name=jack Accept:*/*'
  407         self.assertEqual(self.get_tokens(cmd), [
  408             (Keyword, 'httpie'),
  409             (Name, 'name'), (Operator, '='), (String, 'jack'),
  410             (Name, 'Accept'), (Operator, ':'), (String, '*/*')
  411         ])
  412 
  413     def test_httpie_absolute_url(self):
  414         cmd = 'httpie post http://example.com name=jack'
  415         self.assertEqual(self.get_tokens(cmd), [
  416             (Keyword, 'httpie'), (Keyword, 'post'),
  417             (String, 'http://example.com'),
  418             (Name, 'name'), (Operator, '='), (String, 'jack')
  419         ])
  420 
  421     def test_httpie_option_first(self):
  422         self.assertEqual(self.get_tokens('httpie post --form name=jack'), [
  423             (Keyword, 'httpie'), (Keyword, 'post'),
  424             (Name, '--form'),
  425             (Name, 'name'), (Operator, '='), (String, 'jack')
  426         ])
  427 
  428     def test_httpie_body_param_first(self):
  429         self.assertEqual(self.get_tokens('httpie post name=jack --form'), [
  430             (Keyword, 'httpie'), (Keyword, 'post'),
  431             (Name, 'name'), (Operator, '='), (String, 'jack'),
  432             (Name, '--form')
  433         ])
  434 
  435     def test_httpie_options(self):
  436         self.assertEqual(self.get_tokens('httpie options test --body'), [
  437             (Keyword, 'httpie'), (Keyword, 'options'),
  438             (String, 'test'), (Name, '--body')
  439         ])
  440 
  441     def test_httpie_relative_path(self):
  442         tokens = self.get_tokens('httpie /api/test name==foo',
  443                                  filter_spaces=False)
  444         self.assertEqual(tokens, [
  445             (Keyword, 'httpie'), (Text, ' '),
  446             (String, '/api/test'), (Text, ' '),
  447             (Name, 'name'), (Operator, '=='), (String, 'foo'),
  448             (Text, '\n')
  449         ])
  450 
  451 
  452 class TestShellCode(LexerTestCase):
  453 
  454     def test_unquoted_querystring(self):
  455         self.assertEqual(self.get_tokens('`echo name`==john'), [
  456             (Text, '`'),
  457             (Name.Builtin, 'echo'),
  458             (Text, 'name'),
  459             (Text, '`'),
  460             (Operator, '=='),
  461             (String, 'john')
  462         ])
  463         self.assertEqual(self.get_tokens('name==`echo john`'), [
  464             (Name, 'name'),
  465             (Operator, '=='),
  466             (Text, '`'),
  467             (Name.Builtin, 'echo'),
  468             (Text, 'john'),
  469             (Text, '`')
  470         ])
  471 
  472     def test_unquoted_bodystring(self):
  473         self.assertEqual(self.get_tokens('`echo name`=john'), [
  474             (Text, '`'),
  475             (Name.Builtin, 'echo'),
  476             (Text, 'name'),
  477             (Text, '`'),
  478             (Operator, '='),
  479             (String, 'john')
  480         ])
  481         self.assertEqual(self.get_tokens('name=`echo john`'), [
  482             (Name, 'name'),
  483             (Operator, '='),
  484             (Text, '`'),
  485             (Name.Builtin, 'echo'),
  486             (Text, 'john'),
  487             (Text, '`')
  488         ])
  489 
  490     def test_header_option_value(self):
  491         self.assertEqual(self.get_tokens('Accept:`echo "application/json"`'), [
  492             (Name, 'Accept'),
  493             (Operator, ':'),
  494             (Text, '`'),
  495             (Name.Builtin, 'echo'),
  496             (String.Double, '"application/json"'),
  497             (Text, '`'),
  498         ])
  499 
  500     def test_httpie_body_param(self):
  501         self.assertEqual(self.get_tokens('httpie post name=`echo john`'), [
  502             (Keyword, 'httpie'),
  503             (Keyword, 'post'),
  504             (Name, 'name'),
  505             (Operator, '='),
  506             (Text, '`'),
  507             (Name.Builtin, 'echo'),
  508             (Text, 'john'),
  509             (Text, '`'),
  510         ])
  511 
  512     def test_httpie_post_pipe(self):
  513         self.assertEqual(self.get_tokens('httpie post | tee "/tmp/test"'), [
  514             (Keyword, 'httpie'),
  515             (Keyword, 'post'),
  516             (Operator, '|'),
  517             (Text, 'tee'),
  518             (String.Double, '"/tmp/test"'),
  519         ])
  520 
  521     def test_post_pipe(self):
  522         self.assertEqual(self.get_tokens('post | tee "/tmp/test"'), [
  523             (Keyword, 'post'),
  524             (Operator, '|'),
  525             (Text, 'tee'),
  526             (String.Double, '"/tmp/test"'),
  527         ])
  528 
  529 
  530 class TestLexerPreviewRedirection(LexerTestCase):
  531 
  532     def test_httpie_write(self):
  533         self.assertEqual(self.get_tokens('httpie > file.txt'), [
  534             (Keyword, 'httpie'),
  535             (Operator, '>'), (String, 'file.txt')
  536         ])
  537 
  538     def test_httpie_write_without_spaces(self):
  539         self.assertEqual(self.get_tokens('httpie>file.txt'), [
  540             (Keyword, 'httpie'),
  541             (Operator, '>'), (String, 'file.txt')
  542         ])
  543 
  544     def test_httpie_append(self):
  545         self.assertEqual(self.get_tokens('httpie >> file.txt'), [
  546             (Keyword, 'httpie'),
  547             (Operator, '>>'), (String, 'file.txt')
  548         ])
  549 
  550     def test_httpie_append_without_spaces(self):
  551         self.assertEqual(self.get_tokens('httpie>>file.txt'), [
  552             (Keyword, 'httpie'),
  553             (Operator, '>>'), (String, 'file.txt')
  554         ])
  555 
  556     def test_httpie_write_with_post_param(self):
  557         self.assertEqual(self.get_tokens('httpie post name=jack > file.txt'), [
  558             (Keyword, 'httpie'), (Keyword, 'post'),
  559             (Name, 'name'), (Operator, '='), (String, 'jack'),
  560             (Operator, '>'), (String, 'file.txt')
  561         ])
  562 
  563     def test_httpie_append_with_post_param(self):
  564         self.assertEqual(self.get_tokens('httpie post name=doe >> file.txt'), [
  565             (Keyword, 'httpie'), (Keyword, 'post'),
  566             (Name, 'name'), (Operator, '='), (String, 'doe'),
  567             (Operator, '>>'), (String, 'file.txt')
  568         ])
  569 
  570     def test_httpie_write_quoted_filename(self):
  571         self.assertEqual(self.get_tokens("httpie > 'my file.txt'"), [
  572             (Keyword, 'httpie'), (Operator, '>'),
  573             (Text, "'"), (String, 'my file.txt'), (Text, "'")
  574         ])
  575 
  576     def test_httpie_append_quoted_filename(self):
  577         self.assertEqual(self.get_tokens('httpie >> "my file.txt"'), [
  578             (Keyword, 'httpie'), (Operator, '>>'),
  579             (Text, '"'), (String, 'my file.txt'), (Text, '"')
  580         ])
  581 
  582     def test_httpie_append_with_many_params(self):
  583         command = ("httpie post --auth user:pass --verify=no  "
  584                    "name='john doe'  page==2 >> file.txt")
  585         self.assertEqual(self.get_tokens(command), [
  586             (Keyword, 'httpie'), (Keyword, 'post'),
  587             (Name, '--auth'), (String, 'user:pass'),
  588             (Name, '--verify'), (Operator, '='), (String, 'no'),
  589             (Name, 'name'), (Operator, '='),
  590             (Text, "'"), (String, 'john doe'), (Text, "'"),
  591             (Name, 'page'), (Operator, '=='), (String, '2'),
  592             (Operator, '>>'), (String, 'file.txt')
  593         ])
  594 
  595     def test_curl_write(self):
  596         self.assertEqual(self.get_tokens('curl > file.txt'), [
  597             (Keyword, 'curl'),
  598             (Operator, '>'), (String, 'file.txt')
  599         ])
  600 
  601     def test_curl_write_without_spaces(self):
  602         self.assertEqual(self.get_tokens('curl>file.txt'), [
  603             (Keyword, 'curl'),
  604             (Operator, '>'), (String, 'file.txt')
  605         ])
  606 
  607     def test_curl_append(self):
  608         self.assertEqual(self.get_tokens('curl >> file.txt'), [
  609             (Keyword, 'curl'),
  610             (Operator, '>>'), (String, 'file.txt')
  611         ])
  612 
  613     def test_curl_append_without_spaces(self):
  614         self.assertEqual(self.get_tokens('curl>>file.txt'), [
  615             (Keyword, 'curl'),
  616             (Operator, '>>'), (String, 'file.txt')
  617         ])
  618 
  619     def test_curl_write_with_post_param(self):
  620         self.assertEqual(self.get_tokens('curl post name=jack > file.txt'), [
  621             (Keyword, 'curl'), (Keyword, 'post'),
  622             (Name, 'name'), (Operator, '='), (String, 'jack'),
  623             (Operator, '>'), (String, 'file.txt')
  624         ])
  625 
  626     def test_curl_append_with_post_param(self):
  627         self.assertEqual(self.get_tokens('curl post name=doe >> file.txt'), [
  628             (Keyword, 'curl'), (Keyword, 'post'),
  629             (Name, 'name'), (Operator, '='), (String, 'doe'),
  630             (Operator, '>>'), (String, 'file.txt')
  631         ])
  632 
  633     def test_curl_write_quoted_filename(self):
  634         self.assertEqual(self.get_tokens("curl > 'my file.txt'"), [
  635             (Keyword, 'curl'), (Operator, '>'),
  636             (Text, "'"), (String, 'my file.txt'), (Text, "'")
  637         ])
  638 
  639     def test_curl_append_quoted_filename(self):
  640         self.assertEqual(self.get_tokens('curl >> "my file.txt"'), [
  641             (Keyword, 'curl'), (Operator, '>>'),
  642             (Text, '"'), (String, 'my file.txt'), (Text, '"')
  643         ])
  644 
  645     def test_curl_append_with_many_params(self):
  646         command = ("curl post --auth user:pass --verify=no  "
  647                    "name='john doe'  page==2 >> file.txt")
  648         self.assertEqual(self.get_tokens(command), [
  649             (Keyword, 'curl'), (Keyword, 'post'),
  650             (Name, '--auth'), (String, 'user:pass'),
  651             (Name, '--verify'), (Operator, '='), (String, 'no'),
  652             (Name, 'name'), (Operator, '='),
  653             (Text, "'"), (String, 'john doe'), (Text, "'"),
  654             (Name, 'page'), (Operator, '=='), (String, '2'),
  655             (Operator, '>>'), (String, 'file.txt')
  656         ])
  657 
  658 
  659 class TestLexerAction(LexerTestCase):
  660 
  661     def test_get(self):
  662         self.assertEqual(self.get_tokens('get'), [
  663             (Keyword, 'get')
  664         ])
  665 
  666     def test_post_with_spaces(self):
  667         self.assertEqual(self.get_tokens('   post  '), [
  668             (Keyword, 'post')
  669         ])
  670 
  671     def test_capital_head(self):
  672         self.assertEqual(self.get_tokens('HEAD'), [
  673             (Keyword, 'HEAD')
  674         ])
  675 
  676     def test_delete_random_capitals(self):
  677         self.assertEqual(self.get_tokens('dElETe'), [
  678             (Keyword, 'dElETe')
  679         ])
  680 
  681     def test_patch(self):
  682         self.assertEqual(self.get_tokens('patch'), [
  683             (Keyword, 'patch')
  684         ])
  685 
  686     def test_get_with_querystring_params(self):
  687         command = 'get page==10 id==200'
  688         self.assertEqual(self.get_tokens(command), [
  689             (Keyword, 'get'),
  690             (Name, 'page'), (Operator, '=='), (String, '10'),
  691             (Name, 'id'), (Operator, '=='), (String, '200')
  692         ])
  693 
  694     def test_capital_get_with_querystring_params(self):
  695         command = 'GET page==10 id==200'
  696         self.assertEqual(self.get_tokens(command), [
  697             (Keyword, 'GET'),
  698             (Name, 'page'), (Operator, '=='), (String, '10'),
  699             (Name, 'id'), (Operator, '=='), (String, '200')
  700         ])
  701 
  702     def test_post_with_body_params(self):
  703         command = 'post name="john doe" username=john'
  704         self.assertEqual(self.get_tokens(command), [
  705             (Keyword, 'post'), (Name, 'name'), (Operator, '='),
  706             (Text, '"'), (String, 'john doe'), (Text, '"'),
  707             (Name, 'username'), (Operator, '='), (String, 'john')
  708         ])
  709 
  710     def test_post_with_spaces_and_body_params(self):
  711         command = '  post   name="john doe"     username=john  '
  712         self.assertEqual(self.get_tokens(command), [
  713             (Keyword, 'post'), (Name, 'name'), (Operator, '='),
  714             (Text, '"'), (String, 'john doe'), (Text, '"'),
  715             (Name, 'username'), (Operator, '='), (String, 'john')
  716         ])
  717 
  718     def test_options(self):
  719         self.assertEqual(self.get_tokens('options'), [
  720             (Keyword, 'options')
  721         ])
  722 
  723     def test_post_relative_path(self):
  724         tokens = self.get_tokens('post /api/test name=foo',
  725                                  filter_spaces=False)
  726         self.assertEqual(tokens, [
  727             (Keyword, 'post'), (Text, ' '),
  728             (String, '/api/test'), (Text, ' '),
  729             (Name, 'name'), (Operator, '='), (String, 'foo'),
  730             (Text, '\n')
  731         ])
  732 
  733 
  734 class TestLexerActionRedirection(LexerTestCase):
  735 
  736     def test_get_write(self):
  737         self.assertEqual(self.get_tokens('get > file.txt'), [
  738             (Keyword, 'get'), (Operator, '>'), (String, 'file.txt')
  739         ])
  740 
  741     def test_get_write_quoted_filename(self):
  742         self.assertEqual(self.get_tokens('get > "/tmp/my file.txt"'), [
  743             (Keyword, 'get'), (Operator, '>'),
  744             (Text, '"'), (String, '/tmp/my file.txt'), (Text, '"')
  745         ])
  746 
  747     def test_get_append(self):
  748         self.assertEqual(self.get_tokens('get >> file.txt'), [
  749             (Keyword, 'get'), (Operator, '>>'), (String, 'file.txt')
  750         ])
  751 
  752     def test_get_append_escaped_filename(self):
  753         self.assertEqual(self.get_tokens(r'get >> /tmp/my\ file.txt'), [
  754             (Keyword, 'get'), (Operator, '>>'),
  755             (String, r'/tmp/my\ file.txt')
  756         ])
  757 
  758     def test_post_append_with_spaces(self):
  759         self.assertEqual(self.get_tokens('   post  >>   file.txt'), [
  760             (Keyword, 'post'), (Operator, '>>'), (String, 'file.txt')
  761         ])
  762 
  763     def test_capital_head_write(self):
  764         self.assertEqual(self.get_tokens('HEAD > file.txt'), [
  765             (Keyword, 'HEAD'), (Operator, '>'), (String, 'file.txt')
  766         ])
  767 
  768     def test_get_append_with_querystring_params(self):
  769         command = 'get page==10 id==200 >> /tmp/file.txt'
  770         self.assertEqual(self.get_tokens(command), [
  771             (Keyword, 'get'),
  772             (Name, 'page'), (Operator, '=='), (String, '10'),
  773             (Name, 'id'), (Operator, '=='), (String, '200'),
  774             (Operator, '>>'), (String, '/tmp/file.txt')
  775         ])
  776 
  777     def test_post_write_escaped_filename_with_body_params(self):
  778         command = r'post name="john doe" username=john > /tmp/my\ file.txt'
  779         self.assertEqual(self.get_tokens(command), [
  780             (Keyword, 'post'), (Name, 'name'), (Operator, '='),
  781             (Text, '"'), (String, 'john doe'), (Text, '"'),
  782             (Name, 'username'), (Operator, '='), (String, 'john'),
  783             (Operator, '>'), (String, r'/tmp/my\ file.txt')
  784         ])
  785 
  786     def test_post_append_with_spaces_and_body_params(self):
  787         command = ' post    name="john doe"  username=john  >> /tmp/file.txt  '
  788         self.assertEqual(self.get_tokens(command), [
  789             (Keyword, 'post'), (Name, 'name'), (Operator, '='),
  790             (Text, '"'), (String, 'john doe'), (Text, '"'),
  791             (Name, 'username'), (Operator, '='), (String, 'john'),
  792             (Operator, '>>'), (String, '/tmp/file.txt')
  793         ])