aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-05-19 13:41:13 +0700
committerShulhan <ms@kilabit.info>2021-05-19 13:41:13 +0700
commitbd51133fbda340542ec29ec1035b028ea112e028 (patch)
tree0e1c63bec6a061baafddc2edcd710ab2d291f157
parent0f2059b07661265439a20d895a7a4481ef12b4c8 (diff)
downloadpakakeh.go-bd51133fbda340542ec29ec1035b028ea112e028.tar.xz
xmlrpc: add method to marshal Response
-rw-r--r--lib/xmlrpc/response.go26
-rw-r--r--lib/xmlrpc/response_test.go52
2 files changed, 77 insertions, 1 deletions
diff --git a/lib/xmlrpc/response.go b/lib/xmlrpc/response.go
index 1e0343de..53d35a1f 100644
--- a/lib/xmlrpc/response.go
+++ b/lib/xmlrpc/response.go
@@ -17,6 +17,32 @@ type Response struct {
IsFault bool
}
+//
+// MarshalText encode the Response instance into XML text.
+//
+func (resp *Response) MarshalText() (out []byte, err error) {
+ var buf bytes.Buffer
+
+ buf.WriteString(xml.Header)
+ buf.WriteString("<methodResponse>")
+
+ if !resp.IsFault {
+ fmt.Fprintf(&buf, "<params><param>%s</param></params>",
+ resp.Param)
+ } else {
+ buf.WriteString("<fault><value><struct>")
+ fmt.Fprintf(&buf, "<member><name>faultCode</name><value><int>%d</int></value></member>",
+ resp.FaultCode)
+ fmt.Fprintf(&buf, "<member><name>faultString</name><value><string>%s</string></value></member>",
+ resp.FaultMessage)
+ buf.WriteString("</struct></value></fault>")
+ }
+
+ buf.WriteString("</methodResponse>")
+
+ return buf.Bytes(), nil
+}
+
func (resp *Response) UnmarshalText(text []byte) (err error) {
var (
logp = "xmlrpc: Response"
diff --git a/lib/xmlrpc/response_test.go b/lib/xmlrpc/response_test.go
index 6a6f9e0f..23c61312 100644
--- a/lib/xmlrpc/response_test.go
+++ b/lib/xmlrpc/response_test.go
@@ -5,12 +5,60 @@
package xmlrpc
import (
+ "encoding/xml"
"testing"
"github.com/shuLhan/share/lib/test"
)
-func TestResponse(t *testing.T) {
+func TestResponse_MarshalText(t *testing.T) {
+ cases := []struct {
+ desc string
+ resp *Response
+ exp string
+ }{{
+ desc: "With param",
+ resp: &Response{
+ Param: &Value{
+ Kind: Boolean,
+ In: true,
+ },
+ },
+ exp: xml.Header + `<methodResponse><params><param><value><boolean>true</boolean></value></param></params></methodResponse>`,
+ }, {
+ desc: "With fault",
+ resp: &Response{
+ FaultCode: 404,
+ FaultMessage: "Not found",
+ IsFault: true,
+ },
+ exp: xml.Header + `<methodResponse>` +
+ `<fault><value><struct>` +
+ `<member>` +
+ `<name>faultCode</name>` +
+ `<value><int>404</int></value>` +
+ `</member>` +
+ `<member>` +
+ `<name>faultString</name>` +
+ `<value><string>Not found</string></value>` +
+ `</member>` +
+ `</struct></value></fault>` +
+ `</methodResponse>`,
+ }}
+
+ for _, c := range cases {
+ t.Logf(c.desc)
+
+ got, err := c.resp.MarshalText()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ test.Assert(t, "MarshalText", c.exp, string(got))
+ }
+}
+
+func TestResponse_UnmarshalText(t *testing.T) {
cases := []struct {
desc string
text string
@@ -91,6 +139,8 @@ func TestResponse(t *testing.T) {
}}
for _, c := range cases {
+ t.Logf(c.desc)
+
var got Response
err := got.UnmarshalText([]byte(c.text))