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

Add tests for translator.py #40

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[report]
omit =
*argparse*
*mock*
*funcsigs*
*pbr*
*six*
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock==1.3.0
230 changes: 230 additions & 0 deletions tests/test_translator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import argparse
import os
import unittest

import mock

from http_request_translator import translator


Expand Down Expand Up @@ -277,6 +281,232 @@ 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):
# Arguments
args = argparse.Namespace(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly what I was trying to avoid. Using a hardcoded Namespace means that we'll have to update this in sync with any changes to made to the parser defined in take_arguments. This also prevents testing of the parser.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this can be avoided by making a function over it, and passing only the required attributes.
Everything else, would be set to None. Then the updation part will be bypassed.
We can write separate tests for parser(which is what principle of unit testing says)
What do you think ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I get what you mean. Can you please just code the function you're talking about. 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm saying, a function like namespace_loader(**kwargs) to which you provide something like

data="sample data",
request="GET HTTP/1.1 Host: www.google.com".
so, the namespace loader returns you an object with the kwargs provided. rest everything can be set to a default value, which is None here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, Yes! That'll surely simplify the code a bit, but still, keeping all this separate from the actual parser feels a bit odd to me, tbh. :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you want to check through the complete process, is it ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! The parser and the process_arguments functions are too tightly coupled, IMO and testing them separately is a bit weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would completely agree with you on that point. but still the tests shouldn't depend on them, and my aim was to just lay down some cases. Putting some utility functions like namespace_loader should make these tests independent of the way anything's written. Moreover, as far as your reason is concerned to make it go through the parser. the namespace_loader can be written in such a way that it goes through the parser as well. Modularizing the things will definitely help.

interactive=False,
language=None,
proxy=None,
data=None,
request=None,
search_regex=None,
search_string=None,
file=None
)

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 = argparse.Namespace(
interactive=True,
language="bash",
proxy=None,
data=None,
request=None,
search_regex=None,
search_string=None,
file=None
)

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 = argparse.Namespace(
interactive=False,
language=["bash"],
proxy=None,
data=None,
request=request_data,
search_regex=None,
search_string=None,
file=None
)

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:
Copy link
Contributor

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?

f.write(request_data)
f.close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The with statement automatically closes the file so you don't have to do this manually. See: http://effbot.org/zone/python-with-statement.htm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, will update that 😃


args = argparse.Namespace(
interactive=False,
language=["bash"],
proxy=None,
data=None,
request=None,
search_regex=None,
search_string=None,
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 = argparse.Namespace(
interactive=False,
language=None,
proxy=None,
data=None,
request=request_data,
search_regex=None,
search_string=None,
file=None
)

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 = argparse.Namespace(
interactive=False,
language=["bash", "php", "python"],
proxy=None,
data=None,
request=request_data,
search_regex=None,
search_string=None,
file=None
)

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 = argparse.Namespace(
interactive=False,
language=["lua"],
proxy=None,
data=None,
request=request_data,
search_regex=None,
search_string=None,
file=None
)

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 = argparse.Namespace(
interactive=False,
language=["bash"],
proxy=None,
data="sample=1",
request=request_data,
search_regex=None,
search_string=None,
file=None
)

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 = argparse.Namespace(
interactive=False,
language=["bash"],
proxy="http://someproxy.com",
data=None,
request=request_data,
search_regex=None,
search_string=None,
file=None
)

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 = argparse.Namespace(
interactive=False,
language=["bash"],
proxy="127.0.0.1",
data=None,
request=request_data,
search_regex=None,
search_string=None,
file=None
)

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 = argparse.Namespace(
interactive=False,
language=["bash"],
proxy="127.0.0.1:1337",
data=None,
request=request_data,
search_regex=None,
search_string=None,
file=None
)

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 = argparse.Namespace(
interactive=False,
language=["bash"],
proxy="127.0.0.",
data=None,
request=request_data,
search_regex=None,
search_string=None,
file=None
)

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 = argparse.Namespace(
interactive=False,
language=["bash"],
proxy="http://127.0.0.",
data=None,
request=request_data,
search_regex=None,
search_string=None,
file=None
)

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 = argparse.Namespace(
interactive=False,
language=None,
proxy=None,
data=None,
request=request_data,
search_regex=None,
search_string=None,
file=None
)

with self.assertRaises(SystemExit) as exc:
translator.process_arguments(args)

self.assertEqual(exc.exception.code, -1)


if __name__ == '__main__':
unittest.main()