112 lines
3.3 KiB
Go
112 lines
3.3 KiB
Go
package amazon
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.pengzhan.dev/haopengzhan/hireme/internal/crawler/crawlertest"
|
|
)
|
|
|
|
type roundTripFunc func(req *http.Request) *http.Response
|
|
|
|
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
return f(req), nil
|
|
}
|
|
|
|
func TestAmazonCrawler_DiscoverAndFetch(t *testing.T) {
|
|
mockClient := &http.Client{
|
|
Transport: roundTripFunc(func(r *http.Request) *http.Response {
|
|
if strings.Contains(r.URL.Path, "search.json") {
|
|
body := `{
|
|
"hits": 1,
|
|
"jobs": [
|
|
{
|
|
"id": "2500123",
|
|
"title": "Software Development Engineer II - AWS",
|
|
"company_name": "Amazon Web Services, Inc.",
|
|
"city": "Seattle",
|
|
"state": "WA",
|
|
"country_code": "USA",
|
|
"job_path": "/en/jobs/2500123/software-development-engineer-ii-aws"
|
|
}
|
|
]
|
|
}`
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Body: io.NopCloser(bytes.NewBufferString(body)),
|
|
Header: make(http.Header),
|
|
}
|
|
}
|
|
|
|
if strings.Contains(r.URL.Path, "2500123.json") {
|
|
body := `{
|
|
"job": {
|
|
"id": "2500123",
|
|
"title": "Software Development Engineer II - AWS",
|
|
"company_name": "Amazon Web Services, Inc.",
|
|
"location": "Seattle, WA, USA",
|
|
"description": "<p>Come build the future of cloud computing.</p>",
|
|
"basic_qualifications": "<li>3+ years of non-internship professional software development experience</li>",
|
|
"preferred_qualifications": "<li>Experience with distributed systems and Go/Java/C++</li>"
|
|
}
|
|
}`
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Body: io.NopCloser(bytes.NewBufferString(body)),
|
|
Header: make(http.Header),
|
|
}
|
|
}
|
|
|
|
return &http.Response{
|
|
StatusCode: http.StatusNotFound,
|
|
Body: io.NopCloser(bytes.NewBufferString("not found")),
|
|
Header: make(http.Header),
|
|
}
|
|
}),
|
|
}
|
|
|
|
c := NewAmazonCrawlerWithClient(mockClient)
|
|
if !c.CanHandle("amazon") || !c.CanHandle("aws") {
|
|
t.Errorf("CanHandle should support amazon and aws")
|
|
}
|
|
|
|
discovered, err := c.DiscoverJobs(context.Background(), "https://www.amazon.jobs/en/search.json?base_query=Software+Engineer")
|
|
if err != nil {
|
|
t.Fatalf("DiscoverJobs failed: %v", err)
|
|
}
|
|
|
|
if len(discovered) != 1 {
|
|
t.Fatalf("expected 1 discovered job, got %d", len(discovered))
|
|
}
|
|
|
|
if discovered[0].CanonicalID != "amazon:2500123" {
|
|
t.Errorf("unexpected canonical ID: %s", discovered[0].CanonicalID)
|
|
}
|
|
|
|
extracted, err := c.FetchJobDetail(context.Background(), "https://www.amazon.jobs/en/jobs/2500123")
|
|
if err != nil {
|
|
t.Fatalf("FetchJobDetail failed: %v", err)
|
|
}
|
|
|
|
if extracted.Title != "Software Development Engineer II - AWS" {
|
|
t.Errorf("unexpected title: %s", extracted.Title)
|
|
}
|
|
if extracted.Company != "Amazon Web Services, Inc." {
|
|
t.Errorf("unexpected company: %s", extracted.Company)
|
|
}
|
|
}
|
|
|
|
func TestAmazonCrawler_LiveIntegration(t *testing.T) {
|
|
crawlertest.RunLiveIntegration(t, crawlertest.LiveTestConfig{
|
|
PlatformName: "Amazon",
|
|
Crawler: NewAmazonCrawler(),
|
|
TargetURL: "https://www.amazon.jobs/en/search.json?base_query=Software+Development+Engineer&loc_query=Seattle%2C+WA%2C+USA",
|
|
ExpectedCanonicalPrefix: "amazon:",
|
|
ExpectedCompany: "Amazon",
|
|
})
|
|
}
|