0

我目前正在开发一个开源 API 测试套件,该套件与一个名为Postman的很棒的 REST 客户端一起工作。

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

我通过允许(mustache-esque)语法表示响应的动态部分来计算出动态部分。基本上,如果 aa 值被格式化,{{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 代码之后,看起来我有一个尴尬的检查,不会给我预期的结果。上面的功能已经更新,希望现在可以正常工作。

4

1 回答 1

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 中。

于 2013-08-06T17:02:02.347 回答