This repository was archived by the owner on Mar 13, 2025. It is now read-only.
forked from wiremock/go-wiremock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
74 lines (68 loc) · 2.38 KB
/
client_test.go
File metadata and controls
74 lines (68 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package wiremock
import (
"encoding/json"
"fmt"
"os"
"reflect"
"testing"
"time"
)
func TestStubRule_ToJson(t *testing.T) {
newStubRule := NewStubRule("PATCH", URLMatching("/example"))
expectedRequestBody := fmt.Sprintf(`{"uuid":"%s","id":"%s","request":{"method":"PATCH","urlPattern":"/example"},"response":{"status":200}}`, newStubRule.uuid, newStubRule.uuid)
result, err := json.Marshal(newStubRule)
if err != nil {
t.Fatalf("StubRole json.Marshal error: %v", err)
}
if string(result) != expectedRequestBody {
t.Errorf("expected requestBody %q; got %q", expectedRequestBody, string(result))
}
postStubRule := Post(URLPathEqualTo("/example")).
WithQueryParam("firstName", EqualTo("Jhon")).
WithQueryParam("lastName", NotMatching("Black")).
WithQueryParam("nickname", EqualToIgnoreCase("jhonBlack")).
WithBodyPattern(EqualToJson(`{"meta": "information"}`, IgnoreArrayOrder, IgnoreExtraElements)).
WithBodyPattern(Contains("information")).
WithMultipartPattern(
NewMultipartPattern().
WithName("info").
WithHeader("Content-Type", Contains("charset")).
WithBodyPattern(EqualToJson("{}", IgnoreExtraElements)),
).
WithBasicAuth("username", "password").
WithHeader("x-absent", Absent()).
WithCookie("absentcookie", Absent()).
WithHeader("x-session", Matching("^\\S+@\\S+$")).
WithCookie("session", EqualToXml("<xml>")).
WillReturn(
`{"code": 400, "detail": "detail"}`,
map[string]string{"Content-Type": "application/json"},
400,
).
WithFixedDelayMilliseconds(time.Second * 5).
AtPriority(1).
InScenario("Scenario").
WhenScenarioStateIs("Started").
WillSetStateTo("Stopped")
rawExpectedRequestBody, err := os.ReadFile("expected-template.json")
if err != nil {
t.Fatalf("failed to read expected-template.json %v", err)
}
rawResult, err := json.Marshal(postStubRule)
if err != nil {
t.Fatalf("StubRole json.Marshal error: %v", err)
}
var expected map[string]interface{}
err = json.Unmarshal([]byte(fmt.Sprintf(string(rawExpectedRequestBody), postStubRule.uuid, postStubRule.uuid)), &expected)
if err != nil {
t.Fatalf("StubRole json.Unmarshal error: %v", err)
}
var parsedResult map[string]interface{}
err = json.Unmarshal(rawResult, &parsedResult)
if err != nil {
t.Fatalf("StubRole json.Unmarshal error: %v", err)
}
if !reflect.DeepEqual(parsedResult, expected) {
t.Errorf("expected requestBody\n%v\n%v", parsedResult, expected)
}
}