Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add client options testing #562

Merged
merged 1 commit into from
May 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 62 additions & 0 deletions pkg/api/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Copyright 2021 The Sigstore Authors.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright 2021 The Sigstore Authors.
// Copyright 2022 The Sigstore Authors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right we should update the boilerplate everywhere and the boilerplate test to match...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, this is added when you create the file first time.

if is a new file you add the current year

//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and

package api

import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)

func TestUserAgentOption(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("User-Agent") != "foo" {
t.Error(`expected user-agent to be set to "foo"`)
}
}))

lc := NewClient(nil, WithUserAgent("foo"))
c, ok := lc.(*client)
if !ok {
t.Fatal("wrong legacy client implementation")
}

_, err := c.client.Get(ts.URL)
if err != nil {
t.Fatal(err)
}
}

func TestTimeoutOption(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(10 * time.Second)
}))

lc := NewClient(nil, WithTimeout(time.Second))
c, ok := lc.(*client)
if !ok {
t.Fatal("wrong legacy client implementation")
}

_, err := c.client.Get(ts.URL)
if err == nil {
t.Error("expected client to get timeout error on request")
}
if strings.HasPrefix(err.Error(), "context deadline exceeded") {
t.Error("expected client to specifically have a timeout error")
}
}