Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to resolve variables with file data #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions plugin/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ def do_request(block, buf):
variables = dict((m.groups() for m in (GLOBAL_VAR_REGEX.match(l) for l in buf) if m))
variables.update(dict((m.groups() for m in (VAR_REGEX.match(l) for l in block) if m)))

def to_file(expr):
type, arg = FILE_REGEX.match(expr).groups()
arg = arg.replace('\\(', '(').replace('\\)', ')')
try:
return open(arg, 'rb') if type == 'file' else (arg)
except IOError:
raise ValueError('File: %s not found' % arg)

try:
variables.update(dict([(k, to_file(v).read().strip("\n\r\t")) for (k, v) in variables.items() if FILE_REGEX.match(v)]))
except ValueError as err:
print(err)

block = [line for line in block if not is_comment(line) and line.strip() != '']

if len(block) == 0:
Expand Down Expand Up @@ -66,10 +79,6 @@ def do_request(block, buf):
if all([ '=' in l for l in data ]):
# Form data: separate entries into data dict, and files dict
key_value_pairs = dict([ l.split('=', 1) for l in data ])
def to_file(expr):
type, arg = FILE_REGEX.match(expr).groups()
arg = arg.replace('\\(', '(').replace('\\)', ')')
return open(arg, 'rb') if type == 'file' else (arg)

files = dict([(k, to_file(v)) for (k, v) in key_value_pairs.items() if FILE_REGEX.match(v)])
data = dict([(k, v) for (k, v) in key_value_pairs.items() if not FILE_REGEX.match(v)])
Expand Down Expand Up @@ -219,11 +228,37 @@ def test(assertion, test):
], [ '# $global = httpbin.org']))
test(resp['form']['forma'] == 'a', 'Global variables are substituted.')

try:
do_request([
'POST http://$global/get',
'X-Hey-InvalidFile=$invalidFile'
], ['# $invalidFile=!file(#doesnotexist!)'])
test(1 == 2, 'Files doesnt Exist')
except ValueError:
test(1 == 1, 'Files doesnt Exist')

import os
from tempfile import NamedTemporaryFile

SAMPLE_FILE_CONTENT = 'sample file content'

temp_file = NamedTemporaryFile(delete = False)
temp_file.write(SAMPLE_FILE_CONTENT)
temp_file.close()
resp = extract_json(do_request([
'POST http://httpbin.org/post',
'X-Header:$filedata',
'forma=a',
'formb=b',
"formc=!file(%s)" % temp_file.name,
], ['# $filedata = !file(%s)' % temp_file.name]))
test(resp['headers']['X-Header'] == SAMPLE_FILE_CONTENT, 'Headers are passed with variable substitution.')
test(resp['files']['formc'] == SAMPLE_FILE_CONTENT, 'Files given as path are sent properly.')
test(not 'formc' in resp['form'], 'File not included in form data.')
os.unlink(temp_file.name)

SAMPLE_FILE_CONTENT = 'sample file content'

temp_file = NamedTemporaryFile(delete = False)
temp_file.write(SAMPLE_FILE_CONTENT)
temp_file.close()
Expand Down