2 // Copyright (C) 2019-2019 Marcus Rohrmoser, https://code.mro.name/mro/pinboard4shaarli
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.
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/>.
32 "github.com/stretchr/testify/assert"
37 func TestString(t
*testing
.T
) {
40 assert
.Equal(t
, "de", s
[len(s
)-2:], "ach")
43 func TestPath(t
*testing
.T
) {
45 path_info
:= "pinboard4shaarli.cgi"
46 base
, _
:= url
.Parse("https://demo.shaarli.org/pinboard4shaarli.cgi/v1/about/")
47 base
.Path
= path
.Join(base
.Path
[0:len(base
.Path
)-len(path_info
)], "..", "index.php")
48 assert
.Equal(t
, "/index.php", base
.Path
, "ach")
49 assert
.Equal(t
, "https://demo.shaarli.org/index.php", base
.String(), "ach")
51 cgi
:= filepath
.Base("../uhu/pinboard4shaarli.cgi")
52 str
:= "https://demo.shaarli.org/pinboard4shaarli.cgi/v1/about/"
53 idx
:= strings
.LastIndex(str
, cgi
)
54 assert
.Equal(t
, "/v1/about/", str
[idx
+len(cgi
):], "wowo")
57 func TestCgi(t
*testing
.T
) {
60 // rq := httptest.NewRequest(http.MethodGet, "/", nil)
61 ts
:= httptest
.NewServer(http
.HandlerFunc(func(w http
.ResponseWriter
, r
*http
.Request
) {
62 fmt
.Fprintf(w
, "Hello, client, %s\n", r
.URL
.String())
66 // assert.Equal(t, "", ts.Config, "wowo")
67 // assert.Equal(t, "", ts.Config.Addr, "wowo")
68 res
, _
:= http
.Get(ts
.URL
+ "/" + path
.Join("pinboard4shaarli.cgi"))
69 assert
.Equal(t
, http
.StatusOK
, res
.StatusCode
, "wowo")
70 assert
.Equal(t
, int64(37), res
.ContentLength
, "wowo")
71 assert
.Equal(t
, "text/plain; charset=utf-8", res
.Header
.Get("Content-Type"), "wowo")
73 body
, _
:= ioutil
.ReadAll(res
.Body
)
75 assert
.Equal(t
, "Hello, client, /pinboard4shaarli.cgi\n", string(body
), "wowo")
78 func TestBasicAuth(t
*testing
.T
) {
80 url
:= "https://dema:demu@demo.shaarli.org/pinboard4shaarli.cgi/v1/posts/add?url=http://m.heise.de/12"
81 r
, err
:= http
.NewRequest(http
.MethodGet
, url
, nil)
82 assert
.Equal(t
, nil, err
, "wowo")
84 pwd
, _
:= usr
.Password()
85 r
.SetBasicAuth(usr
.Username(), pwd
)
86 uid
, pwd
, ok
:= r
.BasicAuth()
87 assert
.Equal(t
, true, ok
, "wowo")
88 assert
.Equal(t
, "dema", uid
, "wowo")
89 assert
.Equal(t
, "demu", pwd
, "wowo")
92 func TestURL(t
*testing
.T
) {
95 assert
.Equal(t
, "a/b", path
.Join("a", "b", "/"), "ach")
97 u
, _
:= url
.Parse("https://l.mro.name/pinboard.cgi/v1/info")
98 assert
.Equal(t
, "https://l.mro.name/pinboard.cgi/v1/info", u
.String(), "ach")
101 assert
.Equal(t
, "https://l.mro.name/pinboard.cgi/v1/info", base
.String(), "ach")
103 path_info
:= "/v1/info"
104 base
.Path
= base
.Path
[0:len(base
.Path
)-len(path_info
)] + "/../index.php"
105 assert
.Equal(t
, "https://l.mro.name/pinboard.cgi/../index.php", base
.String(), "ach")
109 base
.RawQuery
= v
.Encode()
110 assert
.Equal(t
, "https://l.mro.name/pinboard.cgi/../index.php?post=uhu", base
.String(), "ach")
113 func TestForm(t
*testing
.T
) {
117 f
.Set("login", "u i d")
118 f
.Set("password", "p & =d")
119 r
:= bytes
.NewReader([]byte(f
.Encode()))
120 b
, err
:= ioutil
.ReadAll(r
)
121 assert
.Nil(t
, err
, "zzz")
122 assert
.Equal(t
, "login=u+i+d&password=p+%26+%3Dd", string(b
), "zzz")
125 func TestFormValuesFromHtml(t
*testing
.T
) {
126 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'
127 assert
.Nil(t
, err
, "soso")
128 ips
, _
:= formValuesFromReader(file
, "loginform")
129 assert
.Equal(t
, 40, len(ips
["token"][0]), "form.token")
130 // assert.Equal(t, "", ips["returnurl"][0], "form.returnurl")
131 assert
.Equal(t
, "Login", ips
[""][0], "form.")
132 assert
.Equal(t
, "", ips
["login"][0], "form.login")
133 assert
.Equal(t
, "", ips
["password"][0], "form.password")
134 assert
.Equal(t
, "", ips
["longlastingsession"][0], "form.longlastingsession")
137 file
, err
= os
.Open("testdata/sebsauvage/login.html") // curl --location --output testdata/login.html 'https://demo.shaarli.org/?post=https://demo.mro.name/shaarligo'
138 assert
.Nil(t
, err
, "soso")
139 ips
, _
= formValuesFromReader(file
, "loginform")
140 assert
.Equal(t
, 40, len(ips
["token"][0]), "form.token")
141 // assert.Equal(t, "", ips["returnurl"][0], "form.returnurl")
142 assert
.Equal(t
, "Login", ips
[""][0], "form.")
143 assert
.Equal(t
, "", ips
["login"][0], "form.login")
144 assert
.Equal(t
, "", ips
["password"][0], "form.password")
145 assert
.Equal(t
, "", ips
["longlastingsession"][0], "form.longlastingsession")
148 file
, err
= os
.Open("testdata/bookmark/login.html") // curl --location --output testdata/login.html 'https://demo.shaarli.org/?post=https://demo.mro.name/shaarligo'
149 assert
.Nil(t
, err
, "soso")
150 ips
, _
= formValuesFromReader(file
, "loginform")
151 assert
.Equal(t
, 40, len(ips
["token"][0]), "form.token")
152 // assert.Equal(t, "", ips["returnurl"][0], "form.returnurl")
153 assert
.Equal(t
, "Login", ips
[""][0], "form.")
154 assert
.Equal(t
, "", ips
["login"][0], "form.login")
155 assert
.Equal(t
, "", ips
["password"][0], "form.password")
156 assert
.Equal(t
, "", ips
["longlastingsession"][0], "form.longlastingsession")
159 file
, err
= os
.Open("testdata/shaarligo/linkform.html") // curl --location --output testdata/login.html 'https://demo.shaarli.org/?post=https://demo.mro.name/shaarligo'
160 assert
.Nil(t
, err
, "soso")
161 ips
, _
= formValuesFromReader(file
, "linkform")
162 assert
.Equal(t
, 40, len(ips
["token"][0]), "form.token")
163 assert
.Equal(t
, "", ips
["returnurl"][0], "form.returnurl")
164 assert
.Equal(t
, "20190106_172531", ips
["lf_linkdate"][0], "form.lf_linkdate")
165 // tim, _ := time.ParseInLocation("20060102_150405", "20190106_172531", time.Now().Location())
166 // assert.Equal(t, int64(1546791931), tim.Unix(), "form.lf_linkdate")
167 // assert.Equal(t, time.Unix(1546791931, 0), tim, "form.lf_linkdate")
168 assert
.Equal(t
, "", ips
["lf_url"][0], "form.lf_url")
169 assert
.Equal(t
, "uhu", ips
["lf_title"][0], "form.lf_title")
170 assert
.Equal(t
, "content", ips
["lf_description"][0], "form.lf_description")
171 assert
.Equal(t
, "", ips
["lf_tags"][0], "form.lf_tags")
172 assert
.Equal(t
, "Save", ips
["save_edit"][0], "form.save_edit")
176 <?xml version="1.0" encoding="UTF-8"?>
177 <?xml-stylesheet type='text/xsl' href='./assets/default/de/do-post.xslt'?>
179 must be compatible with https://code.mro.name/mro/Shaarli-API-test/src/master/tests/test-post.sh
180 https://code.mro.name/mro/ShaarliOS/src/1d124e012933d1209d64071a90237dc5ec6372fc/ios/ShaarliOS/API/ShaarliCmd.m#L386
182 <html xmlns="http://www.w3.org/1999/xhtml" xml:base="https://l.mro.name/">
183 <head><title>{ 🔗 🐳 🚀 💫 }</title></head>
185 <ul id="taglist" style="display:none"></ul>
186 <form method="post" name="linkform">
187 <input name="lf_linkdate" type="hidden" value="20190106_172531"/>
188 <input name="lf_url" type="text" value=""/>
189 <input name="lf_title" type="text" value="uhu"/>
190 <textarea name="lf_description" rows="4" cols="25"></textarea>
191 <input name="lf_tags" type="text" data-multiple="data-multiple" value=""/>
192 <input name="lf_private" type="checkbox" value=""/>
193 <input name="save_edit" type="submit" value="Save"/>
194 <input name="cancel_edit" type="submit" value="Cancel"/>
195 <input name="token" type="hidden" value="d9aab65f6ca2462449079d72d5321ebae0ec8325"/>
196 <input name="returnurl" type="hidden" value=""/>
197 <input name="lf_image" type="hidden" value=""/>
204 func TestRailRoad(t
*testing
.T
) {
205 fa
:= func(success
bool) (ret
string, err error
) {
207 return "fail", fmt
.Errorf("failure")
211 fb
:= func(s
string, err0 error
) (int, error
) {
223 assert
.Equal(t
, 2, v
, "success")
224 assert
.Equal(t
, nil, e
, "success")
227 v
, e
:= fb(fa(false))
228 assert
.Equal(t
, 0, v
, "success")
229 assert
.Equal(t
, "failure", e
.Error(), "success")