-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
61 lines (50 loc) · 1.36 KB
/
check.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
package manaba
import (
"fmt"
"io"
"net/http"
"net/url"
"github.com/PuerkitoBio/goquery"
)
func checkResponse(res *http.Response, body io.ReadCloser, stdUrl string, stdTitle string) (*goquery.Document, error) {
Url := res.Request.URL
if u := getUrl(Url); u != stdUrl {
return nil, fmt.Errorf("unexpected url: '%v'", u)
}
doc, err := goquery.NewDocumentFromReader(body)
if err != nil && err != io.EOF {
return nil, e("NewDocumentFromReader", err)
}
title, err := getTitle(doc)
if err != nil {
return nil, e("getTitle", err)
}
if title != stdTitle {
// doc for isIncorrectUsernameOrPassword
return doc, fmt.Errorf("unexpected title: '%v'", title)
}
return doc, nil
}
func checkResult(res *http.Response) error {
Url := res.Request.URL
if u := getUrl(Url); u != gHomeUrl {
return fmt.Errorf("fail to get resource. unexpected url: '%v'", u)
}
return nil
}
func isIncorrectUsernameOrPassword(doc *goquery.Document) bool {
if doc == nil {
return false
}
return doc.Find("#comment").Find("p").Text() == "User Name or password is invalid"
}
func getUrl(u *url.URL) string {
return fmt.Sprintf("%v://%v%v", u.Scheme, u.Host, u.Path)
}
func getTitle(doc *goquery.Document) (string, error) {
title := doc.Find("title")
if l := title.Length(); l != 1 {
return "", fmt.Errorf("the number of title is not 1 but %v", l)
}
return title.Text(), nil
}