cnblogs/pkg/cnblogs_api_test.go

102 lines
3.6 KiB
Go
Raw Permalink Normal View History

2024-07-17 10:36:32 -07:00
package cnblogs_api
import (
"net/http"
"testing"
"time"
)
var client = &http.Client{
Timeout: 120 * time.Second,
}
func TestGetBlogUri(t *testing.T) {
blogApp, err := GetBlogUri(client, "270749")
if err != nil {
t.Error(err)
}
if blogApp == "" {
t.Error("blogApp is empty")
}
t.Log(blogApp)
}
func TestGetBlogHomepage(t *testing.T) {
blogApp, err := GetBlogUri(client, "270749")
if err != nil {
t.Fatal(err)
}
htmlBody, statusCode, err := GetBlogHomepage(client, blogApp, 7)
if EnsureHomepageOK(string(htmlBody)) == false {
t.Fatal("EnsureHomepageOK is false")
}
if err != nil {
t.Fatal(err)
}
if statusCode != 200 {
t.Fatal("statusCode is not 200")
}
t.Log(string(htmlBody))
}
func TestParsePostsURLFromHomepage(t *testing.T) {
blogApp, err := GetBlogUri(client, "270749")
if err != nil {
t.Fatal(err)
}
htmlBody, _, err := GetBlogHomepage(client, blogApp, 1)
if err != nil {
t.Fatal(err)
}
postMetas, err := ParsePostMetasFromHomepage(htmlBody)
if err != nil {
t.Fatal(err)
}
t.Log(postMetas)
}
func TestParseBlogUriByRegex(t *testing.T) {
text := `<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">博客园 - 真幻de现实</title>
<id>uuid:0a75ddf1-c050-403f-937c-cf7790585fb1;id=1780685</id>
<updated>2018-11-28T09:56:51Z</updated>
<author>
<name>真幻de现实</name>
<uri>https://www.cnblogs.com/lummon/</uri>
</author>
<generator>feed.cnblogs.com</generator>
<entry>
<id>https://www.cnblogs.com/lummon/p/10033657.html</id>
<title type="text">EF 基础提供程序在 Open 上失败 - 真幻de现实</title>
<summary type="text">搜来的思路: 客户端以管理员身份运行netsh winsock reset命令作用是重置 Winsock 目录如果一台机器上的Winsock协议配置有问题的话将会导致网络连接等问题就需要用netsh winsock reset命令来重置Winsock目录借以恢复网络这个命令可以重新初始化网</summary>
<published>2018-11-28T09:56:00Z</published>
<updated>2018-11-28T09:56:00Z</updated>
<author>
<name>真幻de现实</name>
<uri>https://www.cnblogs.com/lummon/</uri>
</author>
<link rel="alternate" href="https://www.cnblogs.com/lummon/p/10033657.html" />
<link rel="alternate" type="text/html" href="https://www.cnblogs.com/lummon/p/10033657.html" />
<content type="html">摘要搜来的思路: 客户端以管理员身份运行netsh winsock reset命令作用是重置 Winsock 目录如果一台机器上的Winsock协议配置有问题的话将会导致网络连接等问题就需要用netsh winsock reset命令来重置Winsock目录借以恢复网络这个命令可以重新初始化网 &lt;a href="https://www.cnblogs.com/lummon/p/10033657.html" target="_blank"&gt;阅读全文&lt;/a&gt;</content>
</entry>
<entry>
<id>https://www.cnblogs.com/lummon/p/5950095.html</id>
<title type="text">flexbox学习 - 真幻de现实</title>
<summary type="text">https://philipwalton.github.io/solved-by-flexbox/ http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool http://www.ruanyifeng.co</summary>
<published>2016-10-11T09:24:00Z</published>
<updated>2016-10-11T09:24:00Z</updated>
<author>
<name>真幻de现实</name>
<uri>https://www.cnblogs.com/lummon/</uri>
</author>
<link rel="alternate" href="https://www.cnblogs.com/lummon/p/5950095.html" />`
uri := ParseBlogUriByRegex([]byte(text))
if uri != "https://www.cnblogs.com/lummon/" {
t.Error("uri is not https://www.cnblogs.com/lummon/")
}
}