2013-07-30 80 views
0

我目前正在開發一個開源API測試套件,它與一個叫做Postman的真棒REST客戶端結合使用。動態JSON檢查API測試套件

與我的測試套件我運行一系列的API調用,檢查預期的JSON對象/數組(或其他)的響應。我認爲我有一個很好的結構來比較兩者,同時仍然允許響應/預期是動態的。

我已經通過允許(小鬍子esque)語法表示響應的動態部分來計算動態部分。基本上,如果一個值被格式化爲{{content}},則它會相應地處理它。這裏的不同值將是:NOT_NULL,STRINGARRAY,OBJECTNUMBER

須藤代碼我有這個功能看起來是這樣的:

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 
     } 
    } 
} 

認爲這將正常工作,但我想檢查,看看是否有任何明顯的東西進來的時候我錯過與此檢查器或如果有一個更有效的方式來做這個非常複雜的檢查。

在此先感謝!

UPDATE

通過我的sudo的代碼去之後,它看起來像我有一個尷尬的檢查,不會給我預期的結果。以上功能已更新至,希望現在可以正常工作。

回答

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中如果有人有興趣。