testdata fastexact search response

This commit is contained in:
Bel LaPointe
2022-01-27 14:18:55 -07:00
parent 9ff1302a73
commit 37a18acdfe
3 changed files with 263 additions and 9 deletions

View File

@@ -107,7 +107,20 @@ func (fe FastExact) searchOne(state config.State) ([]Job, error) {
}
func (fe FastExact) newRequest(state config.State) (*http.Request, error) {
return nil, errors.New("not impl: fe.newreq")
zip, ok := config.States[state]
if !ok {
return nil, fmt.Errorf("no configured zip for %s", state)
}
req, err := http.NewRequest(
http.MethodGet,
"https://www.fastexact.com/secure/index.php?page=ajaxListJobs&action=ajax&zipcode="+zip+"&records_per_page=50&distance=300&st_loc_zip=8",
nil,
)
if err != nil {
return nil, err
}
fe.setHeaders(req)
return req, nil
}
func (fe FastExact) doRequest(req *http.Request) (*http.Response, error) {
@@ -121,20 +134,43 @@ func (fe FastExact) parse(resp *http.Response) ([]Job, error) {
type mockFastExactDoer struct{}
func (mock mockFastExactDoer) doRequest(req *http.Request) (*http.Response, error) {
switch req.URL.Path {
case "/secure/index.php":
if req.URL.Query().Get("page") != "userLogin" {
return nil, errors.New("bad query")
}
if req.URL.Path != "/secure/index.php" {
return nil, errors.New("bad path")
}
switch req.URL.Query().Get("page") {
case "userLogin":
if b, _ := ioutil.ReadAll(req.Body); !bytes.Equal(b, []byte(`user_name=u&user_password=p&buttonSubmit=Login`)) {
return nil, errors.New("bad req body")
}
return &http.Response{
Status: http.StatusText(http.StatusOK),
StatusCode: http.StatusOK,
Header: http.Header{"Set-Cookie": []string{"PHPSESSID=8o0slo18q6cr0v5v5k9ohjcg01; path=/"}},
Header: http.Header{"Set-Cookie": []string{"PHPSESSID=SessionFromLogin; path=/"}},
Body: io.NopCloser(bytes.NewReader([]byte{})),
}, nil
case "ajaxListJobs":
if req.URL.Query().Get("action") != "ajax" {
return nil, errors.New("bad query: action should be ajax")
}
if req.URL.Query().Get("records_per_page") != "50" {
return nil, errors.New("bad query: records_per_page should be 50")
}
if req.URL.Query().Get("distance") != "300" {
return nil, errors.New("bad query: distance should be 300")
}
if req.URL.Query().Get("zipcode") == "" {
return nil, errors.New("bad query: zip code empty")
}
b, err := ioutil.ReadFile("./testdata/fastexact_search.xml")
if err != nil {
return nil, err
}
return &http.Response{
Status: http.StatusText(http.StatusOK),
StatusCode: http.StatusOK,
Header: http.Header{"Set-Cookie": []string{"PHPSESSID=SessionFromSearch; path=/"}},
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}
return nil, errors.New("bad path")
return nil, errors.New("bad query")
}