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

Fix error dropping when making requests #280

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions msgraph/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ type Uri struct {
// RetryableErrorHandler ensures that the response is returned after exhausting retries for a request
// We can't return an error here, or net/http will not return the response
func RetryableErrorHandler(resp *http.Response, err error, numTries int) (*http.Response, error) {
if resp == nil {
return nil, err
}

return resp, nil
}

Expand Down
72 changes: 72 additions & 0 deletions msgraph/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package msgraph

import (
"context"
"fmt"
"log"
"net"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
)

func TestClient_GetWithError(t *testing.T) {
// This creates a listener on a random available port.
l, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal(err)
}
port := l.Addr().(*net.TCPAddr).Port
l.Close()

hc := NewClient(VersionBeta)
hc.Endpoint = fmt.Sprintf("https://localhost:%d/", port)
hc.RetryableClient.RetryMax = 2

_, _, _, err = hc.Get(context.Background(), GetHttpRequestInput{
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: "/users/test",
},
})
if err == nil {
t.Error("expected to get an error, got nil")
}
if msg := err.Error(); !strings.Contains(msg, "connect: connection refused") {
log.Fatalf("got %s, want message with 'connection refused'", msg)
}
}

func TestClient_GetWithResponseAndError(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/beta/users/test" {
n, _ := strconv.Atoi(r.FormValue("n"))
if n < 15 {
http.Redirect(w, r, fmt.Sprintf("%s?n=%d", r.URL.Path, 1), http.StatusTemporaryRedirect)
return
}
}
}))
defer ts.Close()

hc := NewClient(VersionBeta)
hc.Endpoint = ts.URL
hc.RetryableClient.RetryMax = 2

_, _, _, err := hc.Get(context.Background(), GetHttpRequestInput{
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: "/users/test",
},
})
if err == nil {
t.Error("expected to get an error, got nil")
}
if msg := err.Error(); !strings.Contains(msg, "stopped after 10 redirects") {
log.Fatalf("got %s, want message with 'stopped after 10 redirects'", msg)
}
}
Loading