-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for URLEncoded request bodies on the requestData field (#52
) * Adds support for URLEncoded request bodies on the requestData field To set a URLEncoded body set bodyType to urlencoded: requestData: bodyType: urlencoded content: a: b c: d e: f To set a JSON body set bodyType to json: requestData: bodyType: json content: a: b c: d e: f In either case the body is formed from the value of the content key. The change is backwards compatible so that the following: requestData: a: b c: d e: A will send a JSON body formed from the value of the requestData key. * Adds queryParameters to example JSON specification. * Adds `requestData` enhancements to JSON and YAML example specs. * Moves KeyValuePairs type into its own module * Adds tests for decoding KeyValuePairs * Moves Payload to its own module and adds documentation. * Adds tests for Payload
- Loading branch information
1 parent
5353210
commit 36b1a32
Showing
9 changed files
with
287 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
-- | A module defining the KeyValuePairs type. This type may be used to | ||
-- represent a structure in a specification that is a collection of | ||
-- key-vaue pairs. For example query parameters and URLEncoded request | ||
-- bodies. | ||
-- | ||
-- The module provides FromJSON and ToJSON instances for KeyValuePairs. | ||
-- Valid KeyValuePairs JSON is a single JSON object where all values | ||
-- are either String, Scienfific or Bool. | ||
|
||
module Testing.CurlRunnings.Internal.KeyValuePairs | ||
( KeyValuePairs (..) | ||
, KeyValuePair (..) | ||
) where | ||
|
||
import Data.Aeson | ||
import Data.Aeson.Types | ||
import qualified Data.ByteString.Lazy as LBS | ||
import Data.HashMap.Strict as H | ||
import qualified Data.Text as T | ||
import Data.Text.Encoding as T | ||
|
||
-- | A container for a list of key-value pairs | ||
newtype KeyValuePairs = KeyValuePairs [KeyValuePair] deriving Show | ||
|
||
-- | A representation of a single key-value pair | ||
data KeyValuePair = KeyValuePair T.Text T.Text deriving Show | ||
|
||
instance ToJSON KeyValuePairs where | ||
toJSON (KeyValuePairs qs) = | ||
object (fmap (\(KeyValuePair k v) -> k .= toJSON v) qs) | ||
|
||
instance FromJSON KeyValuePairs where | ||
parseJSON = withObject "keyValuePairs" parseKeyValuePairs where | ||
parseKeyValuePairs o = KeyValuePairs <$> traverse parseKeyValuePair (H.toList o) | ||
parseKeyValuePair (t, v) = KeyValuePair t <$> parseSingleValueType v | ||
|
||
parseSingleValueType :: Value -> Parser T.Text | ||
parseSingleValueType (Bool b) = parseToText b | ||
parseSingleValueType (String t) = return t | ||
parseSingleValueType (Number n) = parseToText n | ||
parseSingleValueType invalid = typeMismatch "KeyValuePairs" invalid | ||
|
||
parseToText :: (ToJSON a) => a -> Parser T.Text | ||
parseToText = return . T.decodeUtf8 . LBS.toStrict . encode |
Oops, something went wrong.