import mastermind.rules as r

ruleset = [{'url': 'http://localhost:8000/',
            'request': {'skip': True},
            'name': 'foo',
            'response': {'body': 'ok200.json',
                         'headers': {'add': {'X-ustwo-intercepted': 'Yes'}}}}]

rule = {'url': 'http://localhost:8000/',
        'request': {'skip': True},
        'name': 'foo',
        'response': {'body': 'ok200.json',
                     'headers': {'add': {'X-ustwo-intercepted': 'Yes'}}}}


def test_urls_one():
    assert r.urls(ruleset) == ['http://localhost:8000/']

def test_urls_none():
    assert r.urls([]) == []

def test_find_by_url_exact_match():
    assert r.find_by_url('http://localhost:8000/', ruleset) == rule

def test_find_by_url_exact_no_match():
    assert r.find_by_url('http://foo/', ruleset) == None


def test_url():
    assert r.url(rule) == 'http://localhost:8000/'

def test_skip_true():
    assert r.skip(rule) == True

def test_skip_false():
    assert r.skip({'request': {'skip': False}}) == False
    assert r.skip({'request': {}}) == False
    assert r.skip({}) == False

def test_delay():
    assert r.delay({'response': {}}) == None
    assert r.delay({}) == None
    assert r.delay({'response': {'delay': 10}}) == 10


def test_status_code():
    assert r.status_code({'response': {'code': 500}}) == 500
    assert r.status_code({'response': {}}) == None
    assert r.status_code({'response': {'code': '500'}}) == 500

def test_body_filename_exists():
    assert r.body_filename(rule) == 'ok200.json'

def test_body_filename_not_exists():
    assert r.body_filename({'url': 'http://foo'}) == None

def test_method():
    assert r.method({"url": "http://example.org"}) == None
    assert r.method({"url": "http://example.org",
                     "method": "GET"}) == "GET"
    assert r.method({"url": "http://example.org",
                     "method": "post"}) == "POST"

def test_match_rule():
    assert r.match_rule("GET", "http://example.org")({"url": "http://example.org"})
    assert not r.match_rule("GET", "http://example.org/")({"url": "http://example.org"})
    assert r.match_rule("GET", "http://example.org")({"url": "http://example.org",
                                                      "method": "GET"})
    assert r.match_rule("DELETE", "http://example.org")({"url": "http://example.org",
                                                         "method": "delete"})

    assert not r.match_rule("PUT", "http://example.org")({"url": "http://example.org",
                                                          "method": "delete"})

@@ -2559,28 +2559,977 @@
        %22method%22: %22delete%22%7D)%0A
+%0Adef test_select():%0A    assert r.select(%22GET%22, %22http://example.org%22, %5B%5D) == %5B%5D%0A    assert r.select(%22GET%22, %22http://example.org%22,%0A                    %5B%7B%22url%22: %22http://example.org%22,%0A                      %22method%22: %22GET%22%7D%5D) == %5B%7B%22url%22: %22http://example.org%22,%0A                                              %22method%22: %22GET%22%7D%5D%0A    assert r.select(%22GET%22, %22http://example.org%22,%0A                    %5B%7B%22url%22: %22http://example.org%22,%0A                      %22method%22: %22GET%22%7D,%0A                     %7B%22url%22: %22http://example.org%22,%0A                      %22method%22: %22POST%22%7D%5D) == %5B%7B%22url%22: %22http://example.org%22,%0A                                               %22method%22: %22GET%22%7D%5D%0A    assert r.select(%22GET%22, %22http://example.org/foo%22,%0A                    %5B%7B%22url%22: %22http://example.org%22,%0A                      %22method%22: %22GET%22%7D%5D) == %5B%5D%0A    assert r.select(%22POST%22, %22http://example.org%22,%0A                    %5B%7B%22url%22: %22http://example.org%22,%0A                      %22method%22: %22GET%22%7D%5D) == %5B%5D%0A

Add rules.select() to filter rules