Basic HTTP test

This commit is contained in:
VnPower
2024-10-03 19:37:03 +07:00
parent b39b4fcb78
commit b4c01fd8a8
3 changed files with 87 additions and 129 deletions
+2 -2
View File
@@ -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)!
<p>
<a href="https://codeberg.org/vnpower/pixivfe">
<img alt="Get it on Codeberg" src="https://get-it-on.codeberg.org/get-it-on-blue-on-white.png" height="60">
@@ -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/).
-127
View File
@@ -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)
}
}
}
+85
View File
@@ -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)
}
}
}