<commit_before>from __future__ import absolute_import
from nose.tools import assert_equals
from instana import internal_tracer as tracer
import urllib3


class TestUrllib3:
    def setUp(self):
        """ Clear all spans before a test run """
        self.http = urllib3.PoolManager()
        self.recorder = tracer.recorder
        self.recorder.clear_spans()

    def tearDown(self):
        """ Do nothing for now """
        return None

    def test_vanilla_requests(self):
        r = self.http.request('GET', 'http://127.0.0.1:5000/')
        assert_equals(r.status, 200)

    def test_get_request(self):
        span = tracer.start_span("test")
        r = self.http.request('GET', 'http://127.0.0.1:5000/')
        span.finish()

        spans = self.recorder.queued_spans()
        first_span = spans[1]
        second_span = spans[0]

        assert(r)
        assert_equals(200, r.status)
        assert_equals(2, len(spans))
        assert_equals("test", first_span.data.sdk.name)
        assert_equals("urllib3", second_span.n)
        assert_equals(200, second_span.data.http.status)
        assert_equals("http://127.0.0.1:5000/", second_span.data.http.url)
        assert_equals("GET", second_span.data.http.method)

        assert_equals(None, second_span.error)
        assert_equals(None, second_span.ec)

    def test_put_request(self):
        span = tracer.start_span("test")
        r = self.http.request('PUT', 'http://127.0.0.1:5000/notfound')
        span.finish()

        spans = self.recorder.queued_spans()
        first_span = spans[1]
        second_span = spans[0]

        assert(r)
        assert_equals(404, r.status)
        assert_equals(2, len(spans))
        assert_equals("test", first_span.data.sdk.name)
        assert_equals("urllib3", second_span.n)
        assert_equals(404, second_span.data.http.status)
        assert_equals("http://127.0.0.1:5000/notfound", second_span.data.http.url)
        assert_equals("PUT", second_span.data.http.method)
        assert_equals(None, second_span.error)
        assert_equals(None, second_span.ec)<commit_msg>Add tests for errors & exceptions<commit_after>from __future__ import absolute_import
from nose.tools import assert_equals
from instana import internal_tracer as tracer
import urllib3


class TestUrllib3:
    def setUp(self):
        """ Clear all spans before a test run """
        self.http = urllib3.PoolManager()
        self.recorder = tracer.recorder
        self.recorder.clear_spans()

    def tearDown(self):
        """ Do nothing for now """
        return None

    def test_vanilla_requests(self):
        r = self.http.request('GET', 'http://127.0.0.1:5000/')
        assert_equals(r.status, 200)

    def test_get_request(self):
        span = tracer.start_span("test")
        r = self.http.request('GET', 'http://127.0.0.1:5000/')
        span.finish()

        spans = self.recorder.queued_spans()
        first_span = spans[1]
        second_span = spans[0]

        assert(r)
        assert_equals(200, r.status)
        assert_equals(2, len(spans))
        assert_equals("test", first_span.data.sdk.name)
        assert_equals("urllib3", second_span.n)
        assert_equals(200, second_span.data.http.status)
        assert_equals("http://127.0.0.1:5000/", second_span.data.http.url)
        assert_equals("GET", second_span.data.http.method)

        assert_equals(None, second_span.error)
        assert_equals(None, second_span.ec)

    def test_put_request(self):
        span = tracer.start_span("test")
        r = self.http.request('PUT', 'http://127.0.0.1:5000/notfound')
        span.finish()

        spans = self.recorder.queued_spans()
        first_span = spans[1]
        second_span = spans[0]

        assert(r)
        assert_equals(404, r.status)
        assert_equals(2, len(spans))
        assert_equals("test", first_span.data.sdk.name)
        assert_equals("urllib3", second_span.n)
        assert_equals(404, second_span.data.http.status)
        assert_equals("http://127.0.0.1:5000/notfound", second_span.data.http.url)
        assert_equals("PUT", second_span.data.http.method)
        assert_equals(None, second_span.error)
        assert_equals(None, second_span.ec)

    def test_5xx_request(self):
        span = tracer.start_span("test")
        r = self.http.request('GET', 'http://127.0.0.1:5000/504')
        span.finish()

        spans = self.recorder.queued_spans()
        first_span = spans[1]
        second_span = spans[0]

        assert(r)
        assert_equals(504, r.status)
        assert_equals(2, len(spans))
        assert_equals("test", first_span.data.sdk.name)
        assert_equals("urllib3", second_span.n)
        assert_equals(504, second_span.data.http.status)
        assert_equals("http://127.0.0.1:5000/504", second_span.data.http.url)
        assert_equals("GET", second_span.data.http.method)
        assert_equals(True, second_span.error)
        assert_equals(1, second_span.ec)

    def test_exception_logging(self):
        span = tracer.start_span("test")
        try:
            r = self.http.request('GET', 'http://127.0.0.1:5000/exception')
        except Exception:
            pass

        span.finish()

        spans = self.recorder.queued_spans()
        first_span = spans[1]
        second_span = spans[0]

        assert(r)
        assert_equals(500, r.status)
        assert_equals(2, len(spans))
        assert_equals("test", first_span.data.sdk.name)
        assert_equals("urllib3", second_span.n)
        assert_equals(500, second_span.data.http.status)
        assert_equals("http://127.0.0.1:5000/exception", second_span.data.http.url)
        assert_equals("GET", second_span.data.http.method)
        assert_equals(True, second_span.error)
        assert_equals(1, second_span.ec)
{'fraction_non_alphanumeric': 0.12899326541184597, 'fraction_numerical': 0.03816266620618201, 'mean_word_length': 2.6542586750788644, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 14, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7020101', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1883, 'n_words': 299}