Source

ocaml-sip / tests / test_parser.ml

(*********************************************************************************************************************
 *
 *)
open Testdata_messages
open Batteries
open OUnit
open Sip_types

(*********************************************************************************************************************
 * Parsing
 *)
open Testdata_messages.Util

let assert_str_equal msg a b =
    assert_equal ~msg:msg ~printer:(fun x -> x) a b

let assert_strs_equal msg a b =
    assert_equal ~msg:msg ~printer:(fun x -> "[" ^ String.concat "," x ^ "]") a b

let validate_extracted_field request conv field extracted expected =
    let actual_mime      = request#header (String.lowercase field) in
    assert_equal expected (conv actual_mime);
    assert_equal expected extracted

let validate_message request = function
    | DescrMimeField (field, expected) ->
        let actual = request#header field in
        assert_str_equal ("mime field " ^ field) expected actual

    | DescrMimeFields (field, expected) ->
        let actual = request#header_all (String.lowercase field) in
        assert_strs_equal ("mime fields " ^ field) expected actual

    | DescrContentLength expected ->
        let extracted = request#content_length in
        validate_extracted_field request int_of_string "content-length" extracted expected

    | DescrMaxForwards expected ->
        let extracted = request#max_forwards in
        validate_extracted_field request int_of_string "max-forwards" extracted expected

    | DescrSipVersion expected ->
        let extracted = request#sip_version in
        assert_equal ~msg:"incorrect sip version" ~printer:string_of_sip_version expected extracted

let valid_message_parse_test (title, buffer, descr) = 
    "valid message -- " ^ title >::
    begin fun () ->
        let request = Sip_parser.parse_request buffer in
        List.iter (validate_message request) descr
    end

let invalid_message_parse_test (title, buffer) =
    "invalid message -- " ^ title >::
    begin fun () ->
        assert_raises Sip_parser.Invalid_sip_message (fun () -> Sip_parser.parse_request buffer)
    end

let test_suite = "Parser" >:::
      List.map valid_message_parse_test   Valid_messages.messages
    @ List.map invalid_message_parse_test Invalid_messages.messages
;;