Skip to content

Commit

Permalink
test: add extension script error case (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie authored Oct 19, 2023
1 parent 28da89f commit 854f600
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 2 deletions.
49 changes: 49 additions & 0 deletions pkg/download/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,55 @@ func TestDownloader_UpgradeExtension(t *testing.T) {
})
}

func TestDownloader_ResolveWithExtra(t *testing.T) {
setupDownloader(func(downloader *Downloader) {
if _, err := downloader.InstallExtensionByFolder("./testdata/extensions/extra", false); err != nil {
t.Fatal(err)
}
rr, err := downloader.Resolve(&base.Request{
URL: "https://github.com/test",
})
if err != nil {
t.Fatal(err)
}
if len(rr.Res.Files) != 2 {
t.Fatal("resolve error")
}
})
}

func TestDownloader_Extension_Errors(t *testing.T) {
setupDownloader(func(downloader *Downloader) {
if _, err := downloader.InstallExtensionByFolder("./testdata/extensions/script_error", false); err != nil {
t.Fatal(err)
}
rr, err := downloader.Resolve(&base.Request{
URL: "https://github.com/test",
})
if err != nil {
t.Fatal(err)
}
if len(rr.Res.Files) == 2 {
t.Fatal("script error catch failed")
}
})

setupDownloader(func(downloader *Downloader) {
if _, err := downloader.InstallExtensionByFolder("./testdata/extensions/function_error", false); err != nil {
t.Fatal(err)
}
rr, err := downloader.Resolve(&base.Request{
URL: "https://github.com/test",
})
if err != nil {
t.Fatal(err)
}
if len(rr.Res.Files) == 2 {
t.Fatal("function error catch failed")
}
})
}

func TestDownloader_Extension_Settings(t *testing.T) {
setupDownloader(func(downloader *Downloader) {
if _, err := downloader.InstallExtensionByFolder("./testdata/extensions/settings_empty", false); err != nil {
Expand Down
18 changes: 18 additions & 0 deletions pkg/download/testdata/extensions/extra/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
gopeed.events.onResolve(async function (ctx) {
ctx.res = {
name: "test",
files: Array(2).fill(true).map((_, i) => ({
name: `test-${i}.txt`,
size: 1024,
req: {
url: ctx.req.url + "/" + i,
extra: {
headers: {
'User-Agent': ctx.settings.ua,
},
}
}
}),
),
};
});
22 changes: 22 additions & 0 deletions pkg/download/testdata/extensions/extra/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "extra",
"title": "gopeed extension extra test",
"version": "0.0.1",
"scripts": [
{
"event": "onResolve",
"matches": [
"*://github.com/*"
],
"entry": "index.js"
}
],
"settings": [
{
"name": "ua",
"title": "User-Agent",
"type": "string",
"value": "gopeed"
}
]
}
16 changes: 16 additions & 0 deletions pkg/download/testdata/extensions/function_error/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
gopeed.events.onResolve(async function (ctx) {
const aaa = {};
gopeed.logger.info(aaa.bbb.ccc);

ctx.res = {
name: "test",
files: Array(2).fill(true).map((_, i) => ({
name: `test-${i}.txt`,
size: 1024,
req: {
url: ctx.req.url + "/" + i,
}
}),
),
};
});
21 changes: 21 additions & 0 deletions pkg/download/testdata/extensions/function_error/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "function-error",
"title": "gopeed extension function error test",
"version": "0.0.1",
"scripts": [
{
"event": "onResolve",
"matches": [
"*://github.com/*"
],
"entry": "index.js"
}
],
"settings": [
{
"name": "ua",
"title": "User-Agent",
"type": "string"
}
]
}
16 changes: 16 additions & 0 deletions pkg/download/testdata/extensions/script_error/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const aaa = {};
gopeed.logger.info(aaa.bbb.ccc);

gopeed.events.onResolve(async function (ctx) {
ctx.res = {
name: "test",
files: Array(2).fill(true).map((_, i) => ({
name: `test-${i}.txt`,
size: 1024,
req: {
url: ctx.req.url + "/" + i,
}
}),
),
};
});
21 changes: 21 additions & 0 deletions pkg/download/testdata/extensions/script_error/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "script-error",
"title": "gopeed extension script error test",
"version": "0.0.1",
"scripts": [
{
"event": "onResolve",
"matches": [
"*://github.com/*"
],
"entry": "index.js"
}
],
"settings": [
{
"name": "ua",
"title": "User-Agent",
"type": "string"
}
]
}
4 changes: 2 additions & 2 deletions pkg/download/testdata/extensions/storage/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "basic",
"title": "gopeed extension basic test",
"name": "storage",
"title": "gopeed extension storage test",
"version": "0.0.1",
"scripts": [
{
Expand Down

0 comments on commit 854f600

Please sign in to comment.