-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start to implement scrape test using mock lib
- Loading branch information
Thomas von Dein
committed
Dec 28, 2023
1 parent
c1cbce3
commit ecd9ff5
Showing
7 changed files
with
160 additions
and
1 deletion.
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
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,89 @@ | ||
/* | ||
Copyright © 2023 Thomas von Dein | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
Check failure on line 22 in scrape_test.go GitHub Actions / lint
|
||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
) | ||
|
||
type Adsource struct { | ||
uri string | ||
content string | ||
} | ||
|
||
func IoRead(file string) string { | ||
content, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return string(content) | ||
} | ||
|
||
func InitSources(conf *Config) []Adsource { | ||
ads := []Adsource{ | ||
{ | ||
uri: fmt.Sprintf("%s%s?userId=%d", Baseuri, Listuri, conf.User), | ||
content: IoRead("t/adlist1.html"), | ||
}, | ||
{ | ||
uri: fmt.Sprintf("%s%s?userId=%d&pageNum=2", Baseuri, Listuri, conf.User), | ||
content: IoRead("t/adlist2.html"), | ||
}, | ||
{ | ||
uri: fmt.Sprintf("%s%s?userId=%d&pageNum=3", Baseuri, Listuri, conf.User), | ||
content: IoRead("t/adlist3.html"), | ||
}, | ||
} | ||
|
||
for n := 1; n < 10; n++ { | ||
ads = append(ads, Adsource{ | ||
uri: fmt.Sprintf("%s/s-anzeige/ad%d/%d", Baseuri, n, n), | ||
content: IoRead(fmt.Sprintf("t/ad%d.html", n)), // FIXME: add actual ad?.html files | ||
}) | ||
} | ||
|
||
return ads | ||
} | ||
|
||
func SetIntercept(conf *Config) { | ||
ads := InitSources(conf) | ||
|
||
for _, ad := range ads { | ||
httpmock.RegisterResponder("GET", ad.uri, | ||
httpmock.NewStringResponder(200, ad.content)) | ||
} | ||
|
||
} | ||
|
||
func TestStart(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
conf := &Config{User: 1, Outdir: "t/out"} | ||
|
||
SetIntercept(conf) | ||
|
||
if err := Start(conf); err != nil { | ||
t.Errorf("failed: %s", err.Error()) | ||
} | ||
} |
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,32 @@ | ||
<!DOCTYPE html> | ||
<html lang="de" > | ||
<head> | ||
<title>Ad Listing</title> | ||
</head> | ||
<body> | ||
|
||
<h2 class="text-module-begin"> | ||
<a class="ellipsis" | ||
href="/s-anzeige/first-ad/1">First Ad</a> | ||
</h2> | ||
<h2 class="text-module-begin"> | ||
<a class="ellipsis" | ||
href="/s-anzeige/first-ad/2">Second Ad</a> | ||
</h2> | ||
|
||
<h2 class="text-module-begin"> | ||
<a class="ellipsis" | ||
href="/s-anzeige/first-ad/3">Third Ad</a> | ||
</h2> | ||
|
||
<h2 class="text-module-begin"> | ||
<a class="ellipsis" name="2625911449" | ||
href="/s-anzeige/first-ad/4">Fourth Ad</a> | ||
</h2> | ||
|
||
<h2 class="text-module-begin"> | ||
<a class="ellipsis" name="2624220233" | ||
href="/s-anzeige/first-ad/5">Fitth Ad</a> | ||
</h2> | ||
</body> | ||
</html> |
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,26 @@ | ||
<!DOCTYPE html> | ||
<html lang="de" > | ||
<head> | ||
<title>Ad Listing</title> | ||
</head> | ||
<body> | ||
<h2 class="text-module-begin"> | ||
<a class="ellipsis" | ||
href="/s-anzeige/first-ad/6">Sixth Ad</a> | ||
</h2> | ||
<h2 class="text-module-begin"> | ||
<a class="ellipsis" | ||
href="/s-anzeige/first-ad/7">Seventh Ad</a> | ||
</h2> | ||
|
||
<h2 class="text-module-begin"> | ||
<a class="ellipsis" | ||
href="/s-anzeige/first-ad/8">Eighth Ad</a> | ||
</h2> | ||
|
||
<h2 class="text-module-begin"> | ||
<a class="ellipsis" name="2625911449" | ||
href="/s-anzeige/first-ad/9">Ninth Ad</a> | ||
</h2> | ||
</body> | ||
</html> |
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,9 @@ | ||
<!DOCTYPE html> | ||
<html lang="de" > | ||
<head> | ||
<title>Ad Listing</title> | ||
</head> | ||
<body> | ||
<!-- empty -->> | ||
</body> | ||
</html> |