Skip to content

Commit

Permalink
bears/hypertext: Add HTTPoliceLintBear.py
Browse files Browse the repository at this point in the history
Closes #596
  • Loading branch information
yash-nisar committed Aug 2, 2017
1 parent 2bef3df commit 40dd5d4
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 0 deletions.
1 change: 1 addition & 0 deletions bear-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
HTTPolice~=0.5.2
aenum~=2.0.8
apertium-lint~=0.29
autoflake~=0.6.6
Expand Down
39 changes: 39 additions & 0 deletions bears/hypertext/HTTPoliceLintBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from coalib.bearlib.abstractions.Linter import linter
from coalib.settings.Setting import typed_list
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY

from dependency_management.requirements.PipRequirement import PipRequirement


@linter(executable='httpolice',
output_format='regex',
output_regex=r'(?P<severity>E|C|D)\s(?P<message>.*)',
severity_map={'E': RESULT_SEVERITY.MAJOR,
'C': RESULT_SEVERITY.NORMAL,
'D': RESULT_SEVERITY.INFO})
class HTTPoliceLintBear:
"""
HTTPolice is a linter for HTTP requests and responses. It checks them for
conformance to standards and best practices.
"""
LANGUAGES = {'HAR'}
REQUIREMENTS = {PipRequirement('HTTPolice', '0.5.2')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'coala-devel@googlegroups.com'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Formatting', 'Syntax'}
SEE_MORE = 'https://github.com/vfaronov/httpolice'

@staticmethod
def create_arguments(filename, file, config_file,
httpolice_silence_ids: typed_list(str)=[]):
"""
:param httpolice_silence_ids:
Silences the given list of notice IDs. You can get more information
about the available notices and their IDs at
https://httpolice.readthedocs.io/en/stable/notices.html.
"""
args = '-i', 'har', filename
if httpolice_silence_ids:
args += tuple('-s=' + part for part in httpolice_silence_ids)
return args
72 changes: 72 additions & 0 deletions tests/hypertext/HTTPoliceLintBearTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import os
from queue import Queue

from coalib.results.Result import Result
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
from coalib.settings.Section import Section
from coalib.testing.BearTestHelper import generate_skip_decorator
from coalib.testing.LocalBearTestHelper import LocalBearTestHelper

from bears.hypertext.HTTPoliceLintBear import HTTPoliceLintBear


def get_testfile_path(name):
return os.path.join(os.path.dirname(__file__),
'httpolice_test_files',
name)


def load_testfile(name):
with open(get_testfile_path(name)) as f:
return f.readlines()


@generate_skip_decorator(HTTPoliceLintBear)
class HTTPoliceLintBearTest(LocalBearTestHelper):

def setUp(self):
self.uut = HTTPoliceLintBear(Section('name'), Queue())

def test_syntax_error_in_request_target(self):
filename = 'test_syntax_error_in_request_target.har'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('HTTPoliceLintBear',
message='1045 Syntax error in request target',
file=get_testfile_path(filename),
severity=RESULT_SEVERITY.MAJOR)],
filename=get_testfile_path(filename))

def test_bad_header(self):
filename = 'test_bad_header.har'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('HTTPoliceLintBear',
message="1244 TE header can't be used in "
'HTTP/2',
file=get_testfile_path(filename),
severity=RESULT_SEVERITY.MAJOR)],
filename=get_testfile_path(filename))

def test_silence_id_setting(self):
filename = 'test_bad_header.har'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[],
filename=get_testfile_path(filename),
settings={'httpolice_silence_ids': ['1244']})

def test_good_file(self):
filename = 'test_valid_file.har'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[],
filename=get_testfile_path(filename))
37 changes: 37 additions & 0 deletions tests/hypertext/httpolice_test_files/test_bad_header.har
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"_warning": "This is not a real HAR file! It only contains the keys that are interesting to HTTPolice. Please do not use this as an example of a valid HAR file.",
"_expected": [1244],
"log": {
"creator": {"name": "demo"},
"entries": [
{
"request": {
"method": "GET",
"url": "http://example.com/",
"httpVersion": "HTTP/2",
"headers": [
{"name": "Host", "value": "example.com"},
{"name": "User-Agent", "value": "demo"},
{"name": "TE", "value": "gzip, trailers"}
],
"bodySize": 0
},
"response": {
"httpVersion": "HTTP/2",
"status": 200,
"statusText": "",
"headers": [
{"name": "Date", "value": "Thu, 31 Dec 2015 18:26:56 GMT"},
{"name": "Content-Type", "value": "text/plain"},
{"name": "Content-Length", "value": "14"}
],
"bodySize": 14,
"content": {
"size": 14,
"text": "Hello world!\r\n"
}
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"_warning": "This is not a real HAR file! It only contains the keys that are interesting to HTTPolice. Please do not use this as an example of a valid HAR file.",
"_expected": [1045],
"log": {
"creator": {"name": "demo"},
"entries": [
{
"request": {
"method": "GET",
"url": "http://президент.рф/",
"httpVersion": "HTTP/2",
"headers": [
{"name": "User-Agent", "value": "demo"}
],
"bodySize": 0
},
"response": {
"httpVersion": "HTTP/2",
"status": 200,
"statusText": "OK",
"headers": [
{"name": "Date", "value": "Thu, 31 Dec 2015 18:26:56 GMT"},
{"name": "Content-Type", "value": "text/plain"},
{"name": "Content-Length", "value": "14"}
],
"bodySize": 14,
"content": {
"size": 14,
"text": "Hello world!\r\n"
}
}
}
]
}
}
120 changes: 120 additions & 0 deletions tests/hypertext/httpolice_test_files/test_valid_file.har
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"log": {
"version": "1.1",
"creator": {
"name": "Firefox",
"version": "54.0"
},
"browser": {
"name": "Firefox",
"version": "54.0"
},
"pages": [
{
"startedDateTime": "2017-08-01T12:00:14.221+05:30",
"id": "page_1",
"title": "H2O - the optimized HTTP/2 server",
"pageTimings": {
"onContentLoad": -1,
"onLoad": -1
}
}
],
"entries": [
{
"pageref": "page_1",
"startedDateTime": "2017-08-01T12:00:14.221+05:30",
"time": null,
"request": {
"bodySize": 0,
"method": "GET",
"url": "https://h2o.examp1e.net/",
"httpVersion": "",
"headers": [
{
"name": "Host",
"value": "h2o.examp1e.net"
},
{
"name": "User-Agent",
"value": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0"
},
{
"name": "Accept",
"value": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
},
{
"name": "Accept-Language",
"value": "en-US,en;q=0.5"
},
{
"name": "Accept-Encoding",
"value": "gzip, deflate, br"
},
{
"name": "Referer",
"value": "http://httpolice.readthedocs.io/en/stable/quickstart.html"
},
{
"name": "Cookie",
"value": "h2o_casper=AmgAAAAAAAAAAAAYxfEYAAABSA; _ga=GA1.2.1972227297.1501507070; _gid=GA1.2.519295172.1501507070; _gat=1"
},
{
"name": "Connection",
"value": "keep-alive"
},
{
"name": "Upgrade-Insecure-Requests",
"value": "1"
},
{
"name": "If-Modified-Since",
"value": "Sun, 23 Apr 2017 01:40:15 GMT"
},
{
"name": "If-None-Match",
"value": "\"58fc05ff-16d8\""
},
{
"name": "Cache-Control",
"value": "max-age=0"
}
],
"cookies": [],
"queryString": [],
"postData": {
"mimeType": "",
"params": [],
"text": ""
},
"headersSize": 624
},
"response": {
"status": 0,
"statusText": "",
"httpVersion": "",
"headers": [],
"cookies": [],
"content": {
"mimeType": "",
"size": 0,
"encoding": "base64",
"text": ""
},
"redirectURL": "",
"headersSize": -1,
"bodySize": -1
},
"cache": {},
"timings": {
"blocked": 0,
"dns": 0,
"connect": 0,
"send": 0,
"wait": 0,
"receive": 0
}
}
]
}
}

0 comments on commit 40dd5d4

Please sign in to comment.