Skip to content

Commit

Permalink
Merge pull request #19 from aviaviavi/key_match
Browse files Browse the repository at this point in the history
adding keyMatch
  • Loading branch information
aviaviavi authored Jun 20, 2018
2 parents 8c1b2ec + 9992fea commit 7b7e5c0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions curl-runnings.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
--
-- see: https://github.com/sol/hpack
--
-- hash: 7bb221552afcd01d9eb59facb28651cb408688b95fd4fa710adee06dfa6e84ce
-- hash: 1c28f9d3ccc7a7803ced196b66692ba0b0a50b0916199a1f8cb5c5f12474aca3

name: curl-runnings
version: 0.7.0
version: 0.7.1
synopsis: A framework for declaratively writing curl based API tests
description: Please see the README on Github at <https://github.com/aviaviavi/curl-runnings#readme>
category: Testing
Expand Down
6 changes: 5 additions & 1 deletion examples/example-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
requestMethod: GET
expectData:
# In the `contains` case of data validation, a list of matchers is specified. Currently,
# possible types are `valueMatch` | `keyValueMatch`.
# possible types are `keyMatch` | `valueMatch` | `keyValueMatch`.
contains:
# `keyValueMatch` looks for the key/value pair anywhere in the payload
# here, {'okay': true} must be somewhere in the return payload
Expand All @@ -46,6 +46,8 @@
# This can be useful for matching against values where you don't know the key ahead of time,
# or for values in a top level array.
- valueMatch: true
# `keyMatch` searches for a key anywhere in the payload
- keyMatch: okay
expectStatus: 200

- name: test 4
Expand All @@ -60,6 +62,8 @@
key: okay
value: true
- valueMatch: true
# notContains + keyMatch works great for asserting no errors came back
- keyMatch: error
expectStatus: 200

- name: test 5
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: curl-runnings
version: 0.7.0
version: 0.7.1
github: aviaviavi/curl-runnings
license: MIT
author: Avi Press
Expand Down
12 changes: 12 additions & 0 deletions src/Testing/CurlRunnings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ runReplacementsOnSubvalues state =
case runReplacements state v of
Left l -> Left l
Right newVal -> Right $ ValueMatch newVal
KeyMatch k ->
case interpolateQueryString state k of
Left l -> Left l
Right newKey -> Right $ KeyMatch newKey
KeyValueMatch k v ->
case (interpolateQueryString state k, runReplacements state v) of
(Left l, _) -> Left l
Expand Down Expand Up @@ -368,6 +372,8 @@ jsonContains f jsonValue =
f $ \match ->
case match of
ValueMatch subval -> subval `elem` traversedValue
KeyMatch key ->
any (`containsKey` key) traversedValue
KeyValueMatch key subval ->
any (\o -> containsKeyVal o key subval) traversedValue

Expand All @@ -385,6 +391,12 @@ containsKeyVal jsonValue key val = case jsonValue of
Object o -> H.lookup key o == Just val
_ -> False

-- | Does the json value contain the given key value pair?
containsKey :: Value -> T.Text -> Bool
containsKey jsonValue key = case jsonValue of
Object o -> isJust $ H.lookup key o
_ -> False

-- | Fully traverse the json and return a list of all the values
traverseValue :: Value -> [Value]
traverseValue val =
Expand Down
8 changes: 8 additions & 0 deletions src/Testing/CurlRunnings/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ data JsonSubExpr
-- value. The motivation for this field is largely for checking contents of a
-- top level array. It's also useful if you don't know the key ahead of time.
= ValueMatch Value
-- | Assert a key exists anywhere in the json
| KeyMatch T.Text
-- | Assert the key value pair can be found somewhere the json.
| KeyValueMatch { matchKey :: T.Text
, matchValue :: Value }
Expand All @@ -205,6 +207,12 @@ instance FromJSON JsonSubExpr where
in case toParse of
Object o -> KeyValueMatch <$> o .: "key" <*> o .: "value"
_ -> typeMismatch "JsonSubExpr" toParse

| isJust $ H.lookup "keyMatch" v =
let toParse = fromJust $ H.lookup "keyMatch" v
in case toParse of
String s -> return $ KeyMatch s
_ -> typeMismatch "JsonSubExpr" toParse
| isJust $ H.lookup "valueMatch" v = ValueMatch <$> v .: "valueMatch"
parseJSON invalid = typeMismatch "JsonSubExpr" invalid
instance ToJSON JsonSubExpr
Expand Down

0 comments on commit 7b7e5c0

Please sign in to comment.