Skip to content

Commit

Permalink
test: added tests for yahoo client
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Jun 5, 2023
1 parent 96a5563 commit 5e876e8
Show file tree
Hide file tree
Showing 4 changed files with 312 additions and 16 deletions.
39 changes: 24 additions & 15 deletions internal/quote/yahoo/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const (
userAgentClientHintPlatform = "\"Windows\""
)

func New() *resty.Client {
func New(clientMain *resty.Client, clientSession *resty.Client) *resty.Client {

client := resty.New().
client := clientMain.
SetBaseURL("https://query1.finance.yahoo.com").
SetHeader("authority", "query1.finance.yahoo.com").
SetHeader("accept", "*/*").
Expand All @@ -41,7 +41,7 @@ func New() *resty.Client {
OnAfterResponse(func(c *resty.Client, r *resty.Response) error {

if r.IsError() {
return refreshClient(c)
return RefreshSession(c, clientSession)
}

return nil
Expand All @@ -51,24 +51,24 @@ func New() *resty.Client {

}

func refreshClient(c *resty.Client) error {
func RefreshSession(clientMain *resty.Client, clientSession *resty.Client) error {
var err error
var cookies []*http.Cookie
var crumb string

cookies, err = getCookie(c)
cookies, err = getCookie(clientSession)

if err != nil {
return err
}

crumb, err = getCrumb(c, cookies)
crumb, err = getCrumb(clientSession, cookies)

if err != nil {
return err
}

c.
clientMain.
SetCookies(cookies).
SetQueryParam("crumb", crumb)

Expand All @@ -77,7 +77,7 @@ func refreshClient(c *resty.Client) error {

func getCookie(client *resty.Client) ([]*http.Cookie, error) {

res, _ := resty.New().
res, err := client.
SetRedirectPolicy(resty.FlexibleRedirectPolicy(1)).
R().
SetHeader("authority", "finance.yahoo.com").
Expand All @@ -94,26 +94,31 @@ func getCookie(client *resty.Client) ([]*http.Cookie, error) {
SetHeader("user-agent", userAgent).
Get("https://finance.yahoo.com/")

if err != nil && !strings.Contains(err.Error(), "stopped after") {
return nil, fmt.Errorf("error requesting a cookie: %w", err)
}

if isEUConsentRedirect(res) {
return getCookieEU()
return getCookieEU(client)
}

x := res.Cookies()
if !isRequiredCookieSet(res) {
return nil, errors.New("unexpected response from Yahoo API: A3 session cookie missing from response")
}

return res.Cookies(), nil
return x, nil

}

func getCookieEU() ([]*http.Cookie, error) {
func getCookieEU(client *resty.Client) ([]*http.Cookie, error) {

var cookies []*http.Cookie

reCsrfToken := regexp.MustCompile("gcrumb=(?:([A-Za-z0-9_]*))")
reSessionId := regexp.MustCompile("sessionId=(?:([A-Za-z0-9_-]*))")

res1, err1 := resty.New().
res1, err1 := client.
SetRedirectPolicy(resty.FlexibleRedirectPolicy(3)).
R().
SetHeader("authority", "finance.yahoo.com").
Expand Down Expand Up @@ -160,7 +165,7 @@ func getCookieEU() ([]*http.Cookie, error) {
return cookies, fmt.Errorf("no cookies set by finance.yahoo.com")
}

res2, err2 := resty.New().
res2, err2 := client.
SetRedirectPolicy(resty.FlexibleRedirectPolicy(2)).
SetContentLength(true).
R().
Expand Down Expand Up @@ -202,7 +207,7 @@ func getCookieEU() ([]*http.Cookie, error) {
}

func getCrumb(client *resty.Client, cookies []*http.Cookie) (string, error) {
res, _ := client.R().
res, err := client.R().
SetHeader("authority", "query2.finance.yahoo.com").
SetHeader("accept", "*/*").
SetHeader("accept-language", "en-US,en;q=0.9,ja;q=0.8").
Expand All @@ -218,11 +223,15 @@ func getCrumb(client *resty.Client, cookies []*http.Cookie) (string, error) {
SetCookies(cookies).
Get("https://query2.finance.yahoo.com/v1/test/getcrumb")

if err != nil {
return "", fmt.Errorf("error requesting a crumb: %w", err)
}

if !strings.HasPrefix(res.Status(), "2") {
return "", fmt.Errorf("unexpected response from Yahoo API when attempting to retrieve crumb: non-2xx response code: %s", res.Status())
}

return res.String(), nil
return res.String(), err
}

func isRequiredCookieSet(res *resty.Response) bool {
Expand Down
29 changes: 29 additions & 0 deletions internal/quote/yahoo/client/client_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package client_test

import (
"testing"

"github.com/go-resty/resty/v2"
"github.com/jarcoal/httpmock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestClient(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Client Suite")
}

var clientResty = resty.New()

var _ = BeforeSuite(func() {
httpmock.ActivateNonDefault(clientResty.GetClient())
})

var _ = BeforeEach(func() {
httpmock.Reset()
})

var _ = AfterSuite(func() {
httpmock.DeactivateAndReset()
})
Loading

0 comments on commit 5e876e8

Please sign in to comment.