-
Notifications
You must be signed in to change notification settings - Fork 6
/
assert.go
85 lines (71 loc) · 3.1 KB
/
assert.go
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
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2020 StreetByters Community
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
package aduket
import (
"encoding/json"
"encoding/xml"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func (r RequestRecorder) AssertStringBodyEqual(t *testing.T, expectedBody string) bool {
return assert.Equal(t, expectedBody, string(r.Data))
}
func (r RequestRecorder) AssertJSONBodyEqual(t *testing.T, expectedBody interface{}) bool {
expectedBodyBytes, err := json.Marshal(expectedBody)
if err != nil {
t.Error("expected body could not marshaled to json")
}
return assert.Equal(t, string(expectedBodyBytes), string(r.Body))
}
func (r RequestRecorder) AssertXMLBodyEqual(t *testing.T, expectedXMLBody interface{}) bool {
expectedBodyBytes, err := xml.Marshal(expectedXMLBody)
if err != nil {
t.Error("expected body could not marshaled to xml")
}
return assert.Equal(t, string(expectedBodyBytes), string(r.Body))
}
func (r RequestRecorder) AssertParamEqual(t *testing.T, paramName, paramValue string) bool {
return assert.Equal(t, paramValue, r.Params[paramName])
}
func (r RequestRecorder) AssertQueryParamEqual(t *testing.T, queryParamName string, queryParamValues []string) bool {
return assert.Equal(t, queryParamValues, r.QueryParams[queryParamName])
}
func (r RequestRecorder) AssertFormParamEqual(t *testing.T, formParamName string, formValues []string) bool {
return assert.Equal(t, formValues, r.FormParams[formParamName])
}
func (r RequestRecorder) AssertHeaderContains(t *testing.T, expectedHeader http.Header) bool {
return assert.True(t, isHeaderContains(expectedHeader, r.Header))
}
func (r RequestRecorder) AssertNoRequest(t *testing.T) bool {
return assert.False(t, r.isRequestReceived)
}
func isHeaderContains(expectedHeader, actualHeader http.Header) bool {
assertionResult := true
for key, value := range expectedHeader {
headerValue := actualHeader[key]
assertionResult = assertionResult && assert.ObjectsAreEqualValues(headerValue, value)
}
return assertionResult
}
func isJSONEqual(expectedBody interface{}, actualBody Body) (bool, error) {
expectedBytes, err := json.Marshal(expectedBody)
return assert.ObjectsAreEqualValues(expectedBytes, actualBody), err
}
func isXMLEqual(expectedBody interface{}, actualBody Body) (bool, error) {
expectedBytes, err := xml.Marshal(expectedBody)
return assert.ObjectsAreEqualValues(expectedBytes, actualBody), err
}