/v1/get sunshine.
[pin4sha_cgi.git] / pinboard_test.go
blob6be5086e75fdd6f9900f109239cde97dc87e434c
1 //
2 // Copyright (C) 2019-2019 Marcus Rohrmoser, https://code.mro.name/mro/Shaarli-API-test
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 "fmt"
22 "net/url"
23 "os"
25 "github.com/stretchr/testify/assert"
26 "testing"
29 func TestURL(t *testing.T) {
30 t.Parallel()
32 u, _ := url.Parse("https://l.mro.name/pinboard.cgi/v1/info")
33 assert.Equal(t, "https://l.mro.name/pinboard.cgi/v1/info", u.String(), "ach")
35 base := *u
36 assert.Equal(t, "https://l.mro.name/pinboard.cgi/v1/info", base.String(), "ach")
38 path_info := "/v1/info"
39 base.Path = base.Path[0:len(base.Path)-len(path_info)] + "/../index.php"
40 assert.Equal(t, "https://l.mro.name/pinboard.cgi/../index.php", base.String(), "ach")
42 v := url.Values{}
43 v.Set("post", "uhu")
44 base.RawQuery = v.Encode()
45 assert.Equal(t, "https://l.mro.name/pinboard.cgi/../index.php?post=uhu", base.String(), "ach")
48 func TestFormValuesFromHtml(t *testing.T) {
49 file, err := os.Open("testdata/v0.10.2/login.html") // curl --location --output testdata/login.html 'https://demo.shaarli.org/?post=https://demo.mro.name/shaarligo'
50 assert.Nil(t, err, "soso")
51 ips, _ := formValuesFromReader(file, "loginform")
52 assert.Equal(t, 40, len(ips["token"][0]), "form.token")
53 // assert.Equal(t, "", ips["returnurl"][0], "form.returnurl")
54 assert.Equal(t, "Login", ips[""][0], "form.")
55 assert.Equal(t, "", ips["login"][0], "form.login")
56 assert.Equal(t, "", ips["password"][0], "form.password")
57 assert.Equal(t, "", ips["longlastingsession"][0], "form.longlastingsession")
58 file.Close()
60 file, err = os.Open("testdata/sebsauvage/login.html") // curl --location --output testdata/login.html 'https://demo.shaarli.org/?post=https://demo.mro.name/shaarligo'
61 assert.Nil(t, err, "soso")
62 ips, _ = formValuesFromReader(file, "loginform")
63 assert.Equal(t, 40, len(ips["token"][0]), "form.token")
64 // assert.Equal(t, "", ips["returnurl"][0], "form.returnurl")
65 assert.Equal(t, "Login", ips[""][0], "form.")
66 assert.Equal(t, "", ips["login"][0], "form.login")
67 assert.Equal(t, "", ips["password"][0], "form.password")
68 assert.Equal(t, "", ips["longlastingsession"][0], "form.longlastingsession")
69 file.Close()
71 file, err = os.Open("testdata/bookmark/login.html") // curl --location --output testdata/login.html 'https://demo.shaarli.org/?post=https://demo.mro.name/shaarligo'
72 assert.Nil(t, err, "soso")
73 ips, _ = formValuesFromReader(file, "loginform")
74 assert.Equal(t, 40, len(ips["token"][0]), "form.token")
75 // assert.Equal(t, "", ips["returnurl"][0], "form.returnurl")
76 assert.Equal(t, "Login", ips[""][0], "form.")
77 assert.Equal(t, "", ips["login"][0], "form.login")
78 assert.Equal(t, "", ips["password"][0], "form.password")
79 assert.Equal(t, "", ips["longlastingsession"][0], "form.longlastingsession")
80 file.Close()
82 file, err = os.Open("testdata/shaarligo/linkform.html") // curl --location --output testdata/login.html 'https://demo.shaarli.org/?post=https://demo.mro.name/shaarligo'
83 assert.Nil(t, err, "soso")
84 ips, _ = formValuesFromReader(file, "linkform")
85 assert.Equal(t, 40, len(ips["token"][0]), "form.token")
86 assert.Equal(t, "", ips["returnurl"][0], "form.returnurl")
87 assert.Equal(t, "20190106_172531", ips["lf_linkdate"][0], "form.lf_linkdate")
88 assert.Equal(t, "", ips["lf_url"][0], "form.lf_url")
89 assert.Equal(t, "uhu", ips["lf_title"][0], "form.lf_title")
90 assert.Equal(t, "content", ips["lf_description"][0], "form.lf_description")
91 assert.Equal(t, "", ips["lf_tags"][0], "form.lf_tags")
92 assert.Equal(t, "Save", ips["save_edit"][0], "form.save_edit")
93 file.Close()
96 <?xml version="1.0" encoding="UTF-8"?>
97 <?xml-stylesheet type='text/xsl' href='./assets/default/de/do-post.xslt'?>
98 <!--
99 must be compatible with https://code.mro.name/mro/Shaarli-API-test/src/master/tests/test-post.sh
100 https://code.mro.name/mro/ShaarliOS/src/1d124e012933d1209d64071a90237dc5ec6372fc/ios/ShaarliOS/API/ShaarliCmd.m#L386
102 <html xmlns="http://www.w3.org/1999/xhtml" xml:base="https://l.mro.name/">
103 <head><title>{ 🔗 🐳 🚀 💫 }</title></head>
104 <body>
105 <ul id="taglist" style="display:none"></ul>
106 <form method="post" name="linkform">
107 <input name="lf_linkdate" type="hidden" value="20190106_172531"/>
108 <input name="lf_url" type="text" value=""/>
109 <input name="lf_title" type="text" value="uhu"/>
110 <textarea name="lf_description" rows="4" cols="25"></textarea>
111 <input name="lf_tags" type="text" data-multiple="data-multiple" value=""/>
112 <input name="lf_private" type="checkbox" value=""/>
113 <input name="save_edit" type="submit" value="Save"/>
114 <input name="cancel_edit" type="submit" value="Cancel"/>
115 <input name="token" type="hidden" value="d9aab65f6ca2462449079d72d5321ebae0ec8325"/>
116 <input name="returnurl" type="hidden" value=""/>
117 <input name="lf_image" type="hidden" value=""/>
118 </form>
119 </body>
120 </html>
124 func TestRailRoad(t *testing.T) {
125 fa := func(success bool) (ret string, err error) {
126 if !success {
127 return "fail", fmt.Errorf("failure")
129 return "ok", err
131 fb := func(s string, err0 error) (int, error) {
132 if err0 != nil {
133 return 0, err0
135 ret := 1
136 if s == "ok" {
137 ret = 2
139 return ret, err0
142 v, e := fb(fa(true))
143 assert.Equal(t, 2, v, "success")
144 assert.Equal(t, nil, e, "success")
147 v, e := fb(fa(false))
148 assert.Equal(t, 0, v, "success")
149 assert.Equal(t, "failure", e.Error(), "success")