add a http client timeout when connection shaarli.
[pin4sha_cgi.git] / model_test.go
blobeb9248ef760a7df9fc6b4ca931b82d3116ef3292
1 //
2 // Copyright (C) 2019-2019 Marcus Rohrmoser, https://code.mro.name/mro/pinboard4shaarli
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package main
20 import (
21 "bufio"
22 "bytes"
23 "encoding/json"
24 "encoding/xml"
26 "github.com/stretchr/testify/assert"
27 "testing"
30 // api_token {"result":"692D3D4BD4A2825D5A4A"}
31 // add {"result_code":"item already exists"}
32 // {"result_code":"done"}
34 // get {"date":"2018-04-09T19:26:54Z","user":"mro","posts":[]}
36 func xm(o interface{}) string {
37 var b bytes.Buffer
38 w := bufio.NewWriter(&b)
39 enc := xml.NewEncoder(w)
40 enc.Encode(o)
41 enc.Flush()
42 return string(b.Bytes())
45 func js(o interface{}) string {
46 var b bytes.Buffer
47 w := bufio.NewWriter(&b)
48 enc := json.NewEncoder(w)
49 enc.Encode(o)
50 w.Flush()
51 return string(b.Bytes())
54 func TestXmlEncodeResult(t *testing.T) {
55 t.Parallel()
56 r := Result{Code: "done"}
57 assert.Equal(t, "<result code=\"done\"></result>", xm(r), "ach")
58 assert.Equal(t, "{\"result_code\":\"done\"}\n", js(r), "ach")
61 func TestXmlEncodePosts(t *testing.T) {
62 t.Parallel()
64 assert.Equal(t, "<posts user=\"\" dt=\"\"><post href=\"uhu\" description=\"\" extended=\"\" hash=\"\" others=\"0\" tag=\"\" time=\"\"></post><post href=\"aha\" description=\"\" extended=\"\" hash=\"\" others=\"0\" tag=\"\" time=\"\"></post></posts>",
65 xm(Posts{Posts: []Post{
66 Post{Href: "uhu"},
67 Post{Href: "aha"},
68 }}), "ach")
69 assert.Equal(t, "{\"user\":\"\",\"date\":\"\",\"posts\":[{\"href\":\"uhu\",\"description\":\"\",\"extended\":\"\",\"hash\":\"\",\"meta\":\"\",\"others\":0,\"tag\":\"\",\"time\":\"\"},{\"href\":\"aha\",\"description\":\"\",\"extended\":\"\",\"hash\":\"\",\"meta\":\"\",\"others\":0,\"tag\":\"\",\"time\":\"\"}]}\n",
70 js(Posts{Posts: []Post{
71 Post{Href: "uhu"},
72 Post{Href: "aha"},
73 }}), "ach")