forked from m29h/gosoap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xop_test.go
197 lines (148 loc) · 5.75 KB
/
xop_test.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package soap
import (
"mime"
"strings"
"testing"
"github.com/m29h/xml"
"github.com/stretchr/testify/assert"
)
type DataType string
type RunTimeSeriesReportResponse struct {
XMLName xml.Name `xml:"RunTimeSeriesReportResponse"`
*ReportResponse
}
type ReportResponse struct {
*DelegateServiceResponse
Report *Report `xml:"Report,omitempty"`
}
type DelegateServiceResponse struct {
XMLName xml.Name `xml:"delegateServiceResponse"`
*ResponseData
}
type ResponseData struct {
Result Result `xml:"Result,omitempty"`
Errors Errors `xml:"Errors,omitempty"`
RequestProcessingTime int64 `xml:"RequestProcessingTime,omitempty"`
}
type Result string
type Errors struct {
Error []*Error `xml:"Error,omitempty"`
}
type Error struct {
Code string `xml:"Code,omitempty"`
Message string `xml:"Message,omitempty"`
}
type Report struct {
XMLName xml.Name `xml:"Report"`
DataSets struct {
DataSet []*ReportDataSet `xml:"DataSet,omitempty"`
} `xml:"DataSets,omitempty"`
ReportDuration int64 `xml:"ReportDuration,omitempty"`
NumberOfDataSets int32 `xml:"NumberOfDataSets,omitempty"`
}
type ReportDataSet struct {
Columns struct {
Column []*Column `xml:"Column,omitempty"`
} `xml:"Columns,omitempty"`
CsvAttachment *CsvAttachment `xml:"CsvAttachment,omitempty"`
Type string `xml:"Type,omitempty"`
}
type Column struct {
Name string `xml:"Name,omitempty"`
DataType *DataType `xml:"DataType,omitempty"`
StaticValue string `xml:"StaticValue,omitempty"`
DynamicIndex int32 `xml:"DynamicIndex,omitempty"`
}
type CsvAttachment struct {
XMLName xml.Name `xml:"CsvAttachment"`
CsvAttachmentFormat *CsvAttachmentFormat `xml:"CsvAttachmentFormat,omitempty"`
CsvData []byte `xml:"CsvData,omitempty"`
}
type CsvAttachmentFormat struct {
XMLName xml.Name `xml:"CsvAttachmentFormat"`
*CharacterAttachmentFormat
Headers bool `xml:"Headers,omitempty"`
}
type CharacterAttachmentFormat struct {
XMLName xml.Name `xml:"CharacterAttachmentFormat"`
*AttachmentFormat
Encoding string `xml:"Encoding,omitempty"`
}
type CompressionType string
type AttachmentFormat struct {
XMLName xml.Name `xml:"AttachmentFormat"`
Compression *CompressionType `xml:"Compression,omitempty"`
}
const testMultipartWithCSVContentType = `multipart/related;start="<rootpart*[email protected]>";type="application/xop+xml";boundary="uuid:d7287a84-8be6-4284-afeb-26ee43e46edd";start-info="text/xml"`
const testMultipartWithCSV = `--uuid:d7287a84-8be6-4284-afeb-26ee43e46edd
Content-Id: <rootpart*[email protected]>
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"
Content-Transfer-Encoding: binary
<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:RunTimeSeriesReportResponse xmlns:ns2="http://example.com"><Result>Success</Result><RequestProcessingTime>0</RequestProcessingTime><Report><DataSets><DataSet><Columns><Column><Name>Subscriber.Name</Name><DataType>String</DataType><DynamicIndex>0</DynamicIndex></Column><Column><Name>PeriodStart</Name><DataType>Timestamp</DataType><DynamicIndex>1</DynamicIndex></Column><Column><Name>TotalBytes</Name><DataType>Real64</DataType><DynamicIndex>2</DynamicIndex></Column></Columns><CsvAttachment><CsvAttachmentFormat><Compression>None</Compression><Encoding>UTF-8</Encoding><Headers>false</Headers></CsvAttachmentFormat><CsvData><Include xmlns="http://www.w3.org/2004/08/xop/include" href="cid:[email protected]"/></CsvData></CsvAttachment><Type>Historical.TimeSeries.Subscriber.ApplicationProtocol.Basic</Type></DataSet></DataSets><ReportDuration>2230</ReportDuration><NumberOfDataSets>1</NumberOfDataSets></Report></ns2:RunTimeSeriesReportResponse></S:Body></S:Envelope>
--uuid:d7287a84-8be6-4284-afeb-26ee43e46edd
Content-Id: <[email protected]>
Content-Type: text/csv
Content-Transfer-Encoding: binary
tn_prod-e03d921e-ed56-4d51-826d-c54f0288bfef,2019-08-19T10:20:59.000Z,332682498
--uuid:d7287a84-8be6-4284-afeb-26ee43e46edd--`
func TestMultipartResponseWithCSV(t *testing.T) {
testResp := &RunTimeSeriesReportResponse{}
envelope := NewEnvelope(testResp)
mediaType, mediaParams, err := mime.ParseMediaType(testMultipartWithCSVContentType)
assert.Nil(t, err)
assert.True(t, strings.HasPrefix(mediaType, "multipart/"))
err = newXopDecoder(strings.NewReader(testMultipartWithCSV), mediaParams).decode(envelope)
assert.Nil(t, err)
assert.Equal(t, int32(1), testResp.Report.NumberOfDataSets)
assert.Equal(t, "Subscriber.Name", testResp.Report.DataSets.DataSet[0].Columns.Column[0].Name)
assert.Equal(t, "UTF-8", testResp.Report.DataSets.DataSet[0].CsvAttachment.CsvAttachmentFormat.Encoding)
assert.Equal(t, "tn_prod-e03d921e-ed56-4d51-826d-c54f0288bfef,2019-08-19T10:20:59.000Z,332682498\n", string(testResp.Report.DataSets.DataSet[0].CsvAttachment.CsvData))
assert.Equal(t, int32(1), testResp.Report.NumberOfDataSets)
}
func TestGetNameFromTag(t *testing.T) {
var TestGetNameFromTag = []struct {
testName string
tag string
xmlName string
}{
{
testName: "omitted field tag",
tag: "-",
xmlName: "-",
},
{
testName: "tag with everything",
tag: "namespace name,flag1,flag2",
xmlName: "name",
},
{
testName: "missing namespace",
tag: "name,flag1,flag2",
xmlName: "name",
},
{
testName: "missing flags",
tag: "namespace name",
xmlName: "name",
},
{
testName: "flags only",
tag: ",flag1,flag2",
xmlName: "",
},
{
testName: "name only",
tag: "name",
xmlName: "name",
},
{
testName: "nothing",
tag: "",
xmlName: "",
},
}
for _, tt := range TestGetNameFromTag {
xmlName := getNameFromTag(tt.tag)
assert.Equal(t, tt.xmlName, xmlName)
}
}