1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-19 20:57:53 +02:00
goreleaser/internal/pipe/linkedin/client_test.go
Oleksandr Redko c65b2258cb
fix: pass context.Context to HTTP requests (#5289)
This PR adds using `context.Context` in HTTP requests. Changes are
similar to #5232.

Additionally, it enables `noctx` to automatically detect missing
context:
```
internal/pipe/webhook/webhook.go:85:29: should rewrite http.NewRequestWithContext or add (*Request).WithContext (noctx)
internal/pipe/linkedin/client.go:68:27: (*net/http.Client).Get must not be called (noctx)
internal/pipe/linkedin/client.go:101:27: (*net/http.Client).Get must not be called (noctx)
internal/pipe/linkedin/client.go:172:28: (*net/http.Client).Post must not be called (noctx)
```
2024-11-16 10:16:54 -03:00

122 lines
2.4 KiB
Go

package linkedin
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/goreleaser/goreleaser/v2/internal/testctx"
"github.com/stretchr/testify/require"
)
func TestCreateLinkedInClient(t *testing.T) {
tests := []struct {
name string
cfg oauthClientConfig
wantErr error
}{
{
"non-empty context and access token",
oauthClientConfig{
Context: testctx.New(),
AccessToken: "foo",
},
nil,
},
{
"empty context",
oauthClientConfig{
Context: nil,
AccessToken: "foo",
},
fmt.Errorf("context is nil"),
},
{
"empty access token",
oauthClientConfig{
Context: testctx.New(),
AccessToken: "",
},
fmt.Errorf("empty access token"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := createLinkedInClient(tt.cfg)
if tt.wantErr != nil {
require.EqualError(t, err, tt.wantErr.Error())
} else {
require.NoError(t, err)
}
})
}
}
func TestClient_Share(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(rw, `
{
"sub": "foo",
"activity": "123456789"
}
`)
}))
defer server.Close()
c, err := createLinkedInClient(oauthClientConfig{
Context: testctx.New(),
AccessToken: "foo",
})
if err != nil {
t.Fatalf("could not create client: %v", err)
}
c.baseURL = server.URL
link, err := c.Share(context.Background(), "test")
if err != nil {
t.Fatalf("could not share: %v", err)
}
wantLink := "https://www.linkedin.com/feed/update/123456789"
require.Equal(t, wantLink, link)
}
func TestClientLegacyProfile_Share(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/v2/userinfo" {
rw.WriteHeader(http.StatusForbidden)
return
}
// this is the response from /v2/me (legacy as a fallback)
_, _ = io.WriteString(rw, `
{
"id": "foo",
"activity": "123456789"
}
`)
}))
defer server.Close()
c, err := createLinkedInClient(oauthClientConfig{
Context: testctx.New(),
AccessToken: "foo",
})
if err != nil {
t.Fatalf("could not create client: %v", err)
}
c.baseURL = server.URL
link, err := c.Share(context.Background(), "test")
if err != nil {
t.Fatalf("could not share: %v", err)
}
wantLink := "https://www.linkedin.com/feed/update/123456789"
require.Equal(t, wantLink, link)
}