This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from 0xProject/marcin/bugfix
missing test scenarios
- Loading branch information
Showing
3 changed files
with
91 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package proxy | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-http-utils/headers" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPerformGasLeftCallErrors(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("expect error when HTTP status is not 200", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
server := httptest.NewServer( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if assert.Contains(t, r.Header, headers.ContentType) { | ||
assert.Equal(t, "application/json", r.Header.Get(headers.ContentType)) | ||
} | ||
|
||
w.WriteHeader(http.StatusServiceUnavailable) | ||
}), | ||
) | ||
defer server.Close() | ||
|
||
gas, err := performGasLeftCall(context.TODO(), &http.Client{}, server.URL) | ||
|
||
assert.Zero(t, gas) | ||
assert.Error(t, err) | ||
assert.ErrorContains(t, err, "non-200 HTTP response") | ||
}) | ||
|
||
t.Run("expect error when JSON payload is invalid", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
server := httptest.NewServer( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if assert.Contains(t, r.Header, headers.ContentType) { | ||
assert.Equal(t, "application/json", r.Header.Get(headers.ContentType)) | ||
} | ||
|
||
w.Write([]byte(`{{}`)) | ||
w.WriteHeader(http.StatusOK) | ||
}), | ||
) | ||
defer server.Close() | ||
|
||
gas, err := performGasLeftCall(context.TODO(), &http.Client{}, server.URL) | ||
|
||
assert.Zero(t, gas) | ||
assert.Error(t, err) | ||
assert.ErrorContains(t, err, "json.Decode error") | ||
}) | ||
|
||
t.Run("expect error when server timeouts", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
server := httptest.NewServer( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if assert.Contains(t, r.Header, headers.ContentType) { | ||
assert.Equal(t, "application/json", r.Header.Get(headers.ContentType)) | ||
} | ||
<-time.After(time.Second * 3) | ||
|
||
w.WriteHeader(http.StatusServiceUnavailable) | ||
}), | ||
) | ||
defer server.Close() | ||
|
||
timeout, cancel := context.WithTimeout(context.TODO(), time.Second*1) | ||
defer cancel() | ||
|
||
gas, err := performGasLeftCall(timeout, &http.Client{}, server.URL) | ||
|
||
assert.Zero(t, gas) | ||
assert.Error(t, err) | ||
assert.ErrorIs(t, err, context.DeadlineExceeded) | ||
}) | ||
} |