-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(matchers): allow matchers in request fields
- Allows path, query and headers to contain Matchers in order. - Update integration test examples to demonstrate capability - Speeds up test suite significantly BREAKING CHANGE Change updates the field definitions within the Request and Response types (See MatcherMap and MatcherString). Fixes #72, Fixes #73
- Loading branch information
Showing
13 changed files
with
116 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,61 @@ | ||
package dsl | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
// Request is the default implementation of the Request interface. | ||
type Request struct { | ||
Method string `json:"method"` | ||
Path string `json:"path"` | ||
Query string `json:"query,omitempty"` | ||
Headers map[string]string `json:"headers,omitempty"` | ||
Body interface{} `json:"body,omitempty"` | ||
Method string `json:"method"` | ||
Path MatcherString `json:"path"` | ||
Query MatcherMap `json:"query,omitempty"` | ||
Headers MatcherMap `json:"headers,omitempty"` | ||
Body interface{} `json:"body,omitempty"` | ||
} | ||
|
||
// MatcherMap allows a map[string]string-like object | ||
// to also contain complex matchers | ||
type MatcherMap map[string]MatcherString | ||
|
||
// MarshalJSON is a custom encoder for Header type | ||
func (h MatcherMap) MarshalJSON() ([]byte, error) { | ||
obj := map[string]interface{}{} | ||
|
||
for header, value := range h { | ||
obj[header] = toObject([]byte(value)) | ||
} | ||
|
||
return json.Marshal(obj) | ||
} | ||
|
||
// UnmarshalJSON is a custom decoder for Header type | ||
func (h *MatcherMap) UnmarshalJSON(data []byte) error { | ||
if err := json.Unmarshal(data, &h); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// MatcherString allows the use of Matchers in string types | ||
// It convert any matchers in interaction type to abstract JSON objects | ||
// See https://github.com/pact-foundation/pact-go/issues/71 for background | ||
type MatcherString string | ||
|
||
// MarshalJSON is a custom encoder for Header type | ||
func (m MatcherString) MarshalJSON() ([]byte, error) { | ||
var obj interface{} | ||
|
||
obj = toObject([]byte(m)) | ||
|
||
return json.Marshal(obj) | ||
} | ||
|
||
// UnmarshalJSON is a custom decoder for Header type | ||
func (m *MatcherString) UnmarshalJSON(data []byte) error { | ||
if err := json.Unmarshal(data, &m); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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
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