-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add tests for translator.py #40
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[report] | ||
omit = | ||
*argparse* | ||
*mock* | ||
*funcsigs* | ||
*pbr* | ||
*six* |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mock==1.3.0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import os | ||
import unittest | ||
|
||
import mock | ||
|
||
from http_request_translator import translator | ||
from .utils import load_args | ||
|
||
|
||
class TestTranslator(unittest.TestCase): | ||
|
@@ -277,6 +281,105 @@ def test_parse_raw_request_no_path(self): | |
with self.assertRaises(ValueError): | ||
translator.parse_raw_request(raw_request) | ||
|
||
def test_process_arguments_with_no_arguments(self): | ||
args = load_args() | ||
|
||
with self.assertRaises(SystemExit) as exc: | ||
translator.process_arguments(args) | ||
|
||
self.assertEqual(exc.exception.code, -1) | ||
|
||
def test_process_arguments_with_interactive_mode(self): | ||
args = load_args(language=["bash"], interactive=True) | ||
|
||
with self.assertRaises(SystemExit) as exc: | ||
with mock.patch('http_request_translator.translator.take_headers', side_effect=KeyboardInterrupt): | ||
translator.process_arguments(args) | ||
|
||
self.assertEqual(exc.exception.code, 0) | ||
|
||
def test_process_arguments_with_request(self): | ||
request_data = """GET HTTP/1.1\nHost: google.com\nCache-Control: no-cache""" | ||
args = load_args(language=["bash"], request=request_data) | ||
|
||
self.assertEqual(type(translator.process_arguments(args)), type({})) | ||
|
||
def test_process_arguments_with_file(self): | ||
request_data = """GET HTTP/1.1\nHost: google.com\nCache-Control: no-cache""" | ||
with open('temp', 'w+') as f: | ||
f.write(request_data) | ||
|
||
args = load_args(language=["bash"], file='temp') | ||
|
||
self.assertEqual(type(translator.process_arguments(args)), type({})) | ||
os.remove('temp') | ||
|
||
def test_process_arguments_with_no_language(self): | ||
request_data = """GET HTTP/1.1\nHost: google.com\nCache-Control: no-cache""" | ||
args = load_args(request=request_data) | ||
|
||
self.assertEqual(type(translator.process_arguments(args)), type({})) | ||
|
||
def test_process_arguments_with_multiple_language(self): | ||
request_data = """GET HTTP/1.1\nHost: google.com\nCache-Control: no-cache""" | ||
args = load_args(language=["bash", "php", "python"], request=request_data) | ||
|
||
self.assertEqual(type(translator.process_arguments(args)), type({})) | ||
|
||
def test_process_arguments_with_wrong_language(self): | ||
request_data = """GET HTTP/1.1\nHost: google.com\nCache-Control: no-cache""" | ||
args = load_args(language=["lua"], request=request_data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Who knows, maybe one day we will support LUA. Could you use a random-ish value instead? |
||
|
||
with self.assertRaises(ValueError): | ||
translator.process_arguments(args) | ||
|
||
def test_process_arguments_with_data(self): | ||
request_data = """GET HTTP/1.1\nHost: google.com\nCache-Control: no-cache""" | ||
args = load_args(language=["bash"], data="sample=1", request=request_data) | ||
|
||
self.assertEqual(type(translator.process_arguments(args)), type({})) | ||
|
||
def test_process_arguments_with_proxy_with_scheme(self): | ||
request_data = """GET HTTP/1.1\nHost: google.com\nCache-Control: no-cache""" | ||
args = load_args(language=["bash"], proxy="http://someproxy.com", request=request_data) | ||
|
||
self.assertEqual(type(translator.process_arguments(args)), type({})) | ||
|
||
def test_process_arguments_with_proxy_without_scheme_without_port(self): | ||
request_data = """GET HTTP/1.1\nHost: google.com\nCache-Control: no-cache""" | ||
args = load_args(language=["bash"], proxy="127.0.0.1", request=request_data) | ||
|
||
self.assertEqual(type(translator.process_arguments(args)), type({})) | ||
|
||
def test_process_arguments_with_proxy_without_scheme_with_port(self): | ||
request_data = """GET HTTP/1.1\nHost: google.com\nCache-Control: no-cache""" | ||
args = load_args(language=["bash"], proxy="127.0.0.1:1337", request=request_data) | ||
|
||
self.assertEqual(type(translator.process_arguments(args)), type({})) | ||
|
||
def test_process_arguments_with_invalid_proxy_without_scheme(self): | ||
request_data = """GET HTTP/1.1\nHost: google.com\nCache-Control: no-cache""" | ||
args = load_args(language=["bash"], proxy="127.0.0.", request=request_data) | ||
|
||
with self.assertRaises(ValueError): | ||
translator.process_arguments(args) | ||
|
||
def test_process_arguments_with_invalid_proxy_with_scheme(self): | ||
request_data = """GET HTTP/1.1\nHost: google.com\nCache-Control: no-cache""" | ||
args = load_args(language=["bash"], proxy="http://127.0.0.", request=request_data) | ||
|
||
with self.assertRaises(ValueError): | ||
translator.process_arguments(args) | ||
|
||
def test_process_arguments_with_no_data_and_method_post(self): | ||
request_data = """POST HTTP/1.1\nHost: google.com""" | ||
args = load_args(request=request_data) | ||
|
||
with self.assertRaises(SystemExit) as exc: | ||
translator.process_arguments(args) | ||
|
||
self.assertEqual(exc.exception.code, -1) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import argparse | ||
|
||
|
||
def load_args(**kwargs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dufferzafar I was talking about something like this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this definitely improves the code! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and does it help in solving the problem you stated ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not in its current state, no. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. umm. ok can you describe, what are you expecting ideally ? |
||
args = argparse.Namespace( | ||
data=None, | ||
file=None, | ||
interactive=False, | ||
language=None, | ||
proxy=None, | ||
request=None, | ||
search_regex=None, | ||
search_string=None | ||
) | ||
for key, value in kwargs.iteritems(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not python3 compliant. |
||
setattr(args, key, value) | ||
|
||
return args |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use python's tempfile module instead?