-
Notifications
You must be signed in to change notification settings - Fork 55
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
IWF-125: Limit error size return from API #429
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
package interpreter | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/golang/mock/gomock" | ||
"github.com/indeedeng/iwf/gen/iwfidl" | ||
"github.com/indeedeng/iwf/service/common/ptr" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
"time" | ||
) | ||
|
@@ -88,3 +94,141 @@ func createCommands() ([]iwfidl.TimerCommand, []iwfidl.SignalCommand, []iwfidl.I | |
} | ||
return validTimerCommands, validSignalCommands, internalCommands | ||
} | ||
|
||
func TestComposeHttpError_LocalActivity_LongErrorResponse(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of these test initializations are using duplicated logic as well. It'd be nice if these were consolidated. Maybe a function that takes the repeat length and returns the httpResp? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll try to extract as much logic a to helper as I can There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactored There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good |
||
ctrl := gomock.NewController(t) | ||
mockActivityProvider := NewMockActivityProvider(ctrl) | ||
|
||
longError := strings.Repeat("a", 1000) | ||
|
||
httpResp := &http.Response{ | ||
StatusCode: 400, | ||
Body: io.NopCloser(strings.NewReader(longError)), | ||
} | ||
errMsg := "original error message" | ||
err := errors.New(errMsg) | ||
|
||
returnedError := errors.New("test error msg") | ||
mockActivityProvider.EXPECT().NewApplicationError("1st-attempt-failure", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, longError[:50]+"...", errors.New(errMsg[:5]+"..."))).Return(returnedError) | ||
|
||
err = composeHttpError(true, mockActivityProvider, err, httpResp, "test-error-type") | ||
if err != nil { | ||
return | ||
} | ||
|
||
assert.Error(t, err) | ||
assert.Equal(t, returnedError, err) | ||
} | ||
|
||
func TestComposeHttpError_RegularActivity_LongErrorResponse(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockActivityProvider := NewMockActivityProvider(ctrl) | ||
|
||
longError := strings.Repeat("a", 1000) | ||
|
||
httpResp := &http.Response{ | ||
StatusCode: 400, | ||
Body: io.NopCloser(strings.NewReader(longError)), | ||
} | ||
errMsg := "original error message which is very long like this" | ||
err := errors.New(errMsg) | ||
|
||
returnedError := errors.New("test error msg") | ||
mockActivityProvider.EXPECT().NewApplicationError("test-error-type", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, longError[:500]+"...", errors.New(errMsg[:50]+"..."))).Return(returnedError) | ||
|
||
err = composeHttpError(false, mockActivityProvider, err, httpResp, "test-error-type") | ||
if err != nil { | ||
return | ||
} | ||
|
||
assert.Error(t, err) | ||
assert.Equal(t, returnedError, err) | ||
} | ||
|
||
func TestComposeHttpError_LocalActivity_ShortErrorResponse(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockActivityProvider := NewMockActivityProvider(ctrl) | ||
|
||
shortError := strings.Repeat("a", 40) | ||
|
||
httpResp := &http.Response{ | ||
StatusCode: 400, | ||
Body: io.NopCloser(strings.NewReader(shortError)), | ||
} | ||
errMsg := "OK" | ||
err := errors.New(errMsg) | ||
|
||
returnedError := errors.New("test error msg") | ||
mockActivityProvider.EXPECT().NewApplicationError("1st-attempt-failure", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, shortError, errors.New(errMsg))).Return(returnedError) | ||
|
||
err = composeHttpError(true, mockActivityProvider, err, httpResp, "test-error-type") | ||
if err != nil { | ||
return | ||
} | ||
|
||
assert.Error(t, err) | ||
assert.Equal(t, returnedError, err) | ||
} | ||
|
||
func TestComposeHttpError_RegularActivity_ShortErrorResponse(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockActivityProvider := NewMockActivityProvider(ctrl) | ||
|
||
shortError := strings.Repeat("a", 40) | ||
|
||
httpResp := &http.Response{ | ||
StatusCode: 400, | ||
Body: io.NopCloser(strings.NewReader(shortError)), | ||
} | ||
errMsg := "OK" | ||
err := errors.New(errMsg) | ||
|
||
returnedError := errors.New("test error msg") | ||
mockActivityProvider.EXPECT().NewApplicationError("test-error-type", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", httpResp.StatusCode, shortError, errors.New(errMsg))).Return(returnedError) | ||
|
||
err = composeHttpError(false, mockActivityProvider, err, httpResp, "test-error-type") | ||
if err != nil { | ||
return | ||
} | ||
|
||
assert.Error(t, err) | ||
assert.Equal(t, returnedError, err) | ||
} | ||
|
||
func TestComposeHttpError_LocalActivity_NilResponse(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockActivityProvider := NewMockActivityProvider(ctrl) | ||
|
||
errMsg := "OK" | ||
err := errors.New(errMsg) | ||
|
||
returnedError := errors.New("test error msg") | ||
mockActivityProvider.EXPECT().NewApplicationError("1st-attempt-failure", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", 0, "None", errors.New(errMsg))).Return(returnedError) | ||
|
||
err = composeHttpError(true, mockActivityProvider, err, nil, "test-error-type") | ||
if err != nil { | ||
return | ||
} | ||
|
||
assert.Error(t, err) | ||
assert.Equal(t, returnedError, err) | ||
} | ||
|
||
func TestComposeHttpError_RegularActivity_NilResponse(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockActivityProvider := NewMockActivityProvider(ctrl) | ||
|
||
errMsg := "OK" | ||
err := errors.New(errMsg) | ||
|
||
returnedError := errors.New("test error msg") | ||
mockActivityProvider.EXPECT().NewApplicationError("test-error-type", fmt.Sprintf("statusCode: %v, responseBody: %v, errMsg: %v", 0, "None", errors.New(errMsg))).Return(returnedError) | ||
|
||
err = composeHttpError(false, mockActivityProvider, err, nil, "test-error-type") | ||
if err != nil { | ||
return | ||
} | ||
|
||
assert.Error(t, err) | ||
assert.Equal(t, returnedError, err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: maybe nice to create a small func for the repeated code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good