API测试套件的动态JSON检查

时间:2013-07-30 15:01:28

标签: json performance api chai

我目前正在开发一个开源API测试套件,它与名为Postman的令人敬畏的REST客户端配合使用。

使用我的Test Suite,我运行一系列API调用,检查对预期的JSON对象/数组(或其他)的响应。我认为我有一个很好的结构来比较两者,同时仍然允许响应/预期是动态的。

我通过允许(mustache-esque)语法表示响应的动态部分来找出动态部分。基本上,如果某个值的格式为{{content}},则会相应地处理它。此处的不同值为:NOT_NULLSTRINGARRAYOBJECTNUMBER

我对此函数的sudo代码如下所示:

function (response, expected) {
    if expected has `{{}}` syntax {
        switch syntax
            check response type of against expected type of ({{type}})
                set flag if failure
                return
    } else {
        if response is the same type as expected {
            switch based on type of response
                case string
                    check if response/request are equal
                    set flag if failure
                    return
                case array
                    check if response/request are equal
                    set flag if failure
                    return
                case object
                    for key in object
                        call this function with response[key] and request[key]
        } else {
            set flag (failure)
            return
        } 
    } 
}

认为这将正常工作,但我想查看是否有任何明显的事情我错过了这个检查器或是否有一个更有效的方式来做这个漂亮复杂的检查。

提前致谢!

更新

在浏览了我的sudo-code之后,看起来我有一个尴尬的检查,不会给我预期的结果。上面的函数已更新为希望现在正常工作。

1 个答案:

答案 0 :(得分:0)

经过一番研究和反复试验,这就是我想出来的。它可能并不完美,但由于缺乏任何其他建议,这就是我的目标:

checkJSON: (response, expected) ->
    if /{{2}(STRING|NOT_NULL|ARRAY|OBJECT|NUMBER|IGNORE)}{2}/.test(expected)
        switch expected
            when '{{STRING}}'
                console.log 'testing string!'
                unless typeof(response) is 'string'
                    console.log 'TYPE OF WAS SUPPOSED TO BE STRING: '+ typeof(response)
                    @failure = true
                    return
            when '{{NOT_NULL}}'
                console.log 'testing not null!'
                unless response
                    console.log 'TYPE OF WAS SUPPOSED TO BE NOT_NULL: '+ typeof(response)
                    @failure = true
                    return
            when '{{ARRAY}}'
                console.log 'testing array!'
                unless Object::toString.call(response) is '[object Array]'
                    console.log 'TYPE OF WAS SUPPOSED TO BE OBJECT (ARRAY): '+ typeof(response)
                    @failure = true
                    return
            when '{{OBJECT}}'
                console.log 'testing object!'
                unless typeof(response) is 'object'
                    console.log 'TYPE OF WAS SUPPOSED TO BE OBJECT: '+ typeof(response)
                    @failure = true
                    return
            when '{{NUMBER}}'
                console.log 'testing number!'
                unless typeof(response) is 'number'
                    console.log 'TYPE OF WAS SUPPOSED TO BE NUMBER: '+ typeof(response)
                    @failure = true
                    return
            when '{{IGNORE}}'
                console.log 'IGNORING THIS OUTPUT!'
            #not really necessary because of regex, but good to check
            else
                console.log 'HOW DID YOU GET HERE?!?!?!'
                @failure = true
                return
    else
        if typeof(response) is typeof(expected)
            switch typeof(response)
                when 'string', 'number'
                    unless response is expected
                        console.log 'Response did not equal Expected!'
                        @failure = true
                        return
                when 'object'
                    if Object::toString.call(response) is '[object Array]'
                        #compare arrays
                        unless response.length is expected.length
                            console.log response.length +' was supposed to equal '+ expected.length
                            @failure = true
                            return
                        #need to do more checks, but this will work for now...
                        #LINK: http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript
                    else
                        for key of response
                            if typeof(expected[key]) != 'undefined'
                                @checkJSON(response[key], expected[key])
                            else
                                console.log 'KEY WAS INCORRECT!'
                                @failure = true
                                return
        else
            @failure = true
            return

如果有人有兴趣,上面的代码是在coffeeScript中。