<commit_before>#!/usr/bin/env python

import httplib
import json
import sys

base_url = 'localhost'
port = 8080

def do_request(url, method, body):
    try:
        print url, method, ' => ',
        con = httplib.HTTPConnection(base_url, port)
        headers = {'Content-type' : 'application/json'}
        con.request(method, url, json.dumps(body), headers)
        return json.loads(con.getresponse().read())
    except Exception:
        pass

def create_and_delete_session():
    rep1 = do_request('/api/leases', 'POST', {'user' : 'user1', 'path' : '/path/to/repo/1'})
    print 'New session: ', rep1
    token = rep1['session_token']
    rep2 = do_request('/api/leases/' + token, 'DELETE', {})
    print 'End session: ', rep2

def main():
    base_res = '/api/'
    url_resp = [(base_res + '', 'GET', {}),
                (base_res + 'users', 'GET', {}),
                (base_res + 'repos', 'GET', {}),
                (base_res + 'leases', 'GET', {}),
                (base_res + 'leases', 'POST', {'user' : 'user1', 'path' : '/path/to/repo/1'}),
                (base_res + 'leases', 'POST', {'user' : 'bad_user', 'path' : '/path/to/repo/1'}),
                (base_res + 'leases', 'POST', {'user' : 'user1', 'path' : '/bad/path'}),
                (base_res + 'leases', 'POST', {'user' : 'user1', 'path' : '/path/to/repo/1'})]

    create_and_delete_session()

    for (u, m, b) in url_resp:
        rep = do_request(u, m, b)
        print rep


if __name__ == '__main__':
    main()
<commit_msg>api_test.py should not crash when receiving unexpected replies<commit_after>#!/usr/bin/env python

import httplib
import json
import sys

base_url = 'localhost'
port = 8080

def do_request(url, method, body):
    try:
        print url, method, ' => ',
        con = httplib.HTTPConnection(base_url, port)
        headers = {'Content-type' : 'application/json'}
        con.request(method, url, json.dumps(body), headers)
        return json.loads(con.getresponse().read())
    except Exception:
        pass

def create_and_delete_session():
    try:
        rep1 = do_request('/api/leases', 'POST', {'user' : 'user1', 'path' : '/path/to/repo/1'})
        print 'New session: ', rep1
        token = rep1['session_token']
        rep2 = do_request('/api/leases/' + token, 'DELETE', {})
        print 'End session: ', rep2
    except Exception:
        pass

def main():
    base_res = '/api/'
    url_resp = [(base_res + '', 'GET', {}),
                (base_res + 'users', 'GET', {}),
                (base_res + 'repos', 'GET', {}),
                (base_res + 'leases', 'GET', {}),
                (base_res + 'leases', 'POST', {'user' : 'user1', 'path' : '/path/to/repo/1'}),
                (base_res + 'leases', 'POST', {'user' : 'bad_user', 'path' : '/path/to/repo/1'}),
                (base_res + 'leases', 'POST', {'user' : 'user1', 'path' : '/bad/path'}),
                (base_res + 'leases', 'POST', {'user' : 'user1', 'path' : '/path/to/repo/1'})]

    create_and_delete_session()

    for (u, m, b) in url_resp:
        rep = do_request(u, m, b)
        print rep


if __name__ == '__main__':
    main()

{'fraction_non_alphanumeric': 0.1934761752478414, 'fraction_numerical': 0.010873041253597697, 'mean_word_length': 2.441144114411441, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '481454', 'n_tokens_mistral': 1103, 'n_tokens_neox': 1073, 'n_words': 249}