-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
是什么,包含什么改变?: 1. 新增 enid 字段,该字段是 dedao 每本书的唯一 id,既可以用来请求到书的详情页,也可作为每一个 feed 的唯一 id; 2. atom 文件的创建时间、更新时间加以区别; 3. 优化函数注释以及打印日志; 4. main_test.go 增加单元测试函数; 如何解决的问题?: 无 本次提交影响范围: all
- Loading branch information
Showing
3 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFetchBooks(t *testing.T) { | ||
books, err := fetchBooks() | ||
if err != nil { | ||
t.Fatalf("fetchBooks failed: %v", err) | ||
} | ||
|
||
if len(books) != 50 { | ||
t.Fatalf("expected 50 books, got %d", len(books)) | ||
} | ||
} | ||
|
||
func TestGenerateAtom(t *testing.T) { | ||
books := []Book{ | ||
{ | ||
Author: "Author1", | ||
Cover: "http://example.com/cover1.jpg", | ||
Title: "Title1", | ||
AuthorInfo: "Author Info 1", | ||
BookIntro: "Book Intro 1", | ||
PublishTime: "2023-01-01", | ||
Uptime: "2023-01-01 00:00:00", | ||
OtherShareSummary: "Summary 1", | ||
Enid: "enid1", | ||
}, | ||
} | ||
|
||
atom, err := generateAtom(books) | ||
if err != nil { | ||
t.Fatalf("generateAtom failed: %v", err) | ||
} | ||
|
||
assert.Contains(t, atom, "<title>Title1</title>") | ||
assert.Contains(t, atom, "http://example.com/cover1.jpg") | ||
} | ||
|
||
func TestSaveAtomToFile(t *testing.T) { | ||
atom := "<feed><title>Test Feed</title></feed>" | ||
err := saveAtomToFile(atom) | ||
if err != nil { | ||
t.Fatalf("saveAtomToFile failed: %v", err) | ||
} | ||
|
||
data, err := os.ReadFile("dedao.atom") | ||
if err != nil { | ||
t.Fatalf("ReadFile failed: %v", err) | ||
} | ||
|
||
assert.Equal(t, atom, string(data)) | ||
} | ||
|
||
func TestUpdateAtomFile(t *testing.T) { | ||
go updateAtomFile() | ||
|
||
time.Sleep(5 * time.Second) | ||
|
||
data, err := os.ReadFile("dedao.atom") | ||
if err != nil { | ||
t.Fatalf("ReadFile failed: %v", err) | ||
} | ||
|
||
assert.Contains(t, string(data), "<feed xmlns=\"http://www.w3.org/2005/Atom\">") | ||
} | ||
|
||
func TestMainRoute(t *testing.T) { | ||
gin.SetMode(gin.TestMode) | ||
r := gin.Default() | ||
|
||
r.GET("/feeds/dedao.atom", func(c *gin.Context) { | ||
data, err := os.ReadFile("dedao.atom") | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Read Atom file failed"}) | ||
return | ||
} | ||
|
||
c.Header("Content-Type", "application/atom+xml; charset=utf-8") | ||
c.Data(http.StatusOK, "application/atom+xml; charset=utf-8", data) | ||
}) | ||
|
||
req, _ := http.NewRequest("GET", "/feeds/dedao.atom", nil) | ||
w := httptest.NewRecorder() | ||
r.ServeHTTP(w, req) | ||
|
||
assert.Equal(t, http.StatusOK, w.Code) | ||
assert.Contains(t, w.Body.String(), "<feed xmlns=\"http://www.w3.org/2005/Atom\">") | ||
} |