github.com/windmeup/goreleaser@v1.21.95/internal/pipe/linkedin/client_test.go (about) 1 package linkedin 2 3 import ( 4 "fmt" 5 "io" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 "github.com/windmeup/goreleaser/internal/testctx" 12 ) 13 14 func TestCreateLinkedInClient(t *testing.T) { 15 tests := []struct { 16 name string 17 cfg oauthClientConfig 18 wantErr error 19 }{ 20 { 21 "non-empty context and access token", 22 oauthClientConfig{ 23 Context: testctx.New(), 24 AccessToken: "foo", 25 }, 26 nil, 27 }, 28 { 29 "empty context", 30 oauthClientConfig{ 31 Context: nil, 32 AccessToken: "foo", 33 }, 34 fmt.Errorf("context is nil"), 35 }, 36 { 37 "empty access token", 38 oauthClientConfig{ 39 Context: testctx.New(), 40 AccessToken: "", 41 }, 42 fmt.Errorf("empty access token"), 43 }, 44 } 45 for _, tt := range tests { 46 t.Run(tt.name, func(t *testing.T) { 47 _, err := createLinkedInClient(tt.cfg) 48 if tt.wantErr != nil { 49 require.EqualError(t, err, tt.wantErr.Error()) 50 } else { 51 require.NoError(t, err) 52 } 53 }) 54 } 55 } 56 57 func TestClient_Share(t *testing.T) { 58 server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { 59 _, _ = io.WriteString(rw, ` 60 { 61 "id": "foo", 62 "activity": "123456789" 63 } 64 `) 65 })) 66 defer server.Close() 67 68 c, err := createLinkedInClient(oauthClientConfig{ 69 Context: testctx.New(), 70 AccessToken: "foo", 71 }) 72 if err != nil { 73 t.Fatalf("could not create client: %v", err) 74 } 75 76 c.baseURL = server.URL 77 78 link, err := c.Share("test") 79 if err != nil { 80 t.Fatalf("could not share: %v", err) 81 } 82 83 wantLink := "https://www.linkedin.com/feed/update/123456789" 84 require.Equal(t, wantLink, link) 85 }