From b4c01fd8a8dcda921b4f2ec8fc9444d50f395eed Mon Sep 17 00:00:00 2001 From: VnPower Date: Thu, 3 Oct 2024 19:37:03 +0700 Subject: [PATCH] Basic HTTP test --- README.md | 4 +- test/basic_test.go | 127 --------------------------------------------- test/http_test.go | 85 ++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 129 deletions(-) delete mode 100644 test/basic_test.go create mode 100644 test/http_test.go diff --git a/README.md b/README.md index f4fb2ea..68c3f39 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ A open source alternative frontend for Pixiv. +**Notice**: PixivFE now has a public room on Matrix, [join here](https://matrix.to/#/!YqHdIPECWMDHWvoxTi:exozy.me)! +

Get it on Codeberg @@ -14,8 +16,6 @@ A open source alternative frontend for Pixiv. [![Docker Pulls](https://img.shields.io/docker/pulls/vnpower/pixivfe)](https://hub.docker.com/r/vnpower/pixivfe) [![Docker Stars](https://img.shields.io/docker/stars/vnpower/pixivfe)](https://hub.docker.com/r/vnpower/pixivfe) -**Notice**: PixivFE now has a public room on Matrix, [join here](https://matrix.to/#/!YqHdIPECWMDHWvoxTi:exozy.me)! - Questions? Feedback? You can [PM me](https://matrix.to/#/@vnpower:matrix.4d2.org) on Matrix! You can also see the [Known quirks](https://pixivfe-docs.pages.dev/known-quirks/) page to check if your issue has a known solution. You can keep track of this project's development using the [Roadmap](https://pixivfe-docs.pages.dev/dev/roadmap/). diff --git a/test/basic_test.go b/test/basic_test.go deleted file mode 100644 index 12ab6b6..0000000 --- a/test/basic_test.go +++ /dev/null @@ -1,127 +0,0 @@ -package main_test - -import ( - "log" - "testing" - - "github.com/playwright-community/playwright-go" -) - -var pw *playwright.Playwright -var browser playwright.Browser - -func setup() { - var err error - - runOption := &playwright.RunOptions{ - SkipInstallBrowsers: true, - } - if err = playwright.Install(runOption); err != nil { - log.Fatalf("could not install playwright dependencies. %s", err) - } - - pw, err = playwright.Run() - if err != nil { - log.Fatalf("could not start playwright") - } - - option := playwright.BrowserTypeLaunchOptions{ - Channel: playwright.String("firefox"), - } - - browser, err = pw.Firefox.Launch(option) - if err != nil { - log.Fatalf("could not launch browser: %v", err) - } - - log.Print("Setup is complete") -} - -func teardown() { - if err := browser.Close(); err != nil { - log.Fatalf("could not close browser: %v", err) - } - - if err := pw.Stop(); err != nil { - log.Fatalf("could not stop Playwright: %v", err) - } - log.Print("Teardown is complete") -} - -// TestMain can be used for global setup and teardown -func TestMain(m *testing.M) { - setup() - _ = m.Run() - teardown() -} - -func getBaseURL() string { - return "http://0.0.0.0:8282" - // return "https://pixivfe.exozy.me" -} - -func checkIfPageHasError(page playwright.Page) bool { - hasErr, _ := page.Locator(".error").IsVisible() - - return hasErr -} - -func TestBasicGetHomepage(t *testing.T) { - page, err := browser.NewPage() - if err != nil { - t.Errorf("could not create page: %v", err) - } - if _, err = page.Goto(getBaseURL() + "/"); err != nil { - t.Errorf("could not goto: %v", err) - } - artworks, err := page.Locator(".artwork-small").All() - if err != nil { - t.Errorf("could not get entries: %v", err) - } - - if len(artworks) != 50 { - t.Errorf("number of daily ranking artworks is %d. expected: 50", len(artworks)) - } -} - -func TestBasicAllRoutes(t *testing.T) { - page, err := browser.NewPage() - if err != nil { - t.Errorf("could not create page: %v", err) - } - - testPositiveURLs := []string{ - // Discovery pages - "/discovery", - "/discovery?mode=r18", - "/discovery/novel", - "/discovery/novel?mode=r18", - - // Ranking pages - "/ranking", - "/ranking?content=all&date=20230212&page=1&mode=male", - "/ranking?content=mangas&date=20221022&page=3&mode=weekly_r18", - "/ranking?content=ugoira&date=&page=1&mode=daily_r18", - "/rankingCalendar?mode=daily_r18&date=2018-08-01", - - // Artwork page - "/artworks/121247335", - "/artworks/120131626", // NSFW - "/artworks-multi/121289276,121247331,121200724", - - // Users page - "/users/810305", - "/users/810305/novels", - "/users/810305/bookmarks", - } - - for _, url := range testPositiveURLs { - if _, err = page.Goto(getBaseURL() + url); err != nil { - t.Errorf("could not goto: %v", err) - } - - if checkIfPageHasError(page) { - log.Fatalf("Path's response NOT OK: %s", url) - } - } -} diff --git a/test/http_test.go b/test/http_test.go new file mode 100644 index 0000000..9f629b9 --- /dev/null +++ b/test/http_test.go @@ -0,0 +1,85 @@ +package main_test + +import ( + "testing" + "net/http" + "io" + "log" +) + +func setup() { +} + +func teardown() { +} + +// TestMain can be used for global setup and teardown +func TestMain(m *testing.M) { + setup() + _ = m.Run() + teardown() +} + +func getBaseURL() string { + return "http://0.0.0.0:8282" + // return "https://pixivfe.exozy.me" +} + +func generateRequest(link, method string, body io.Reader) *http.Request { + req, err := http.NewRequest(method, link, body) + if err != nil { + log.Fatalf("Failed to generate a request: %s", err) + } + + req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0") + + return req +} + +func executeRequest(req *http.Request) *http.Response { + resp, err := http.DefaultClient.Do(req) + if err != nil { + log.Fatalf("Failed to execute a request: %s", err) + } + + return resp +} + +func TestBasicAllRoutes(t *testing.T) { + testPositiveURLs := []string{ + // Discovery pages + "/discovery", + "/discovery?mode=r18", + "/discovery/novel", + "/discovery/novel?mode=r18", + + // Ranking pages + "/ranking", + "/ranking?content=all&date=20230212&page=1&mode=male", + "/ranking?content=manga&page=2&mode=weekly_r18", + "/ranking?content=ugoira&mode=daily_r18", + "/rankingCalendar?mode=daily_r18&date=2018-08-01", + + // Artwork page + "/artworks/121247335", + "/artworks/120131626", // NSFW + "/artworks-multi/121289276,121247331,121200724", + + // Users page + "/users/810305", + "/users/810305/novels", + "/users/810305/bookmarks", + } + + for _, path := range testPositiveURLs { + URL := getBaseURL() + path + t.Logf("GETting: %s", URL) + req := generateRequest(URL, "GET", nil) + resp := executeRequest(req) + + + if resp.StatusCode != 200 { + t.Errorf("Request route response NOT OK: %d", resp.StatusCode) + } + } +}