Merge pull request #111 from yunify/strict-http-status-validate
strict-http-status-validate
This commit is contained in:
commit
bc1811248c
|
@ -76,29 +76,40 @@ func (u *Unpacker) parseResponse() error {
|
||||||
return fmt.Errorf("API Gateway output format error")
|
return fmt.Errorf("API Gateway output format error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
u.httpResponse.Body.Close()
|
||||||
|
err := fmt.Errorf("Response StatusCode: %d", u.httpResponse.StatusCode)
|
||||||
|
logger.Error(err.Error())
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Unpacker) parseError() error {
|
func (u *Unpacker) parseError() error {
|
||||||
|
if u.output.IsNil() {
|
||||||
|
return fmt.Errorf("nil returned")
|
||||||
|
}
|
||||||
retCodeValue := u.output.Elem().FieldByName("RetCode")
|
retCodeValue := u.output.Elem().FieldByName("RetCode")
|
||||||
messageValue := u.output.Elem().FieldByName("Message")
|
messageValue := u.output.Elem().FieldByName("Message")
|
||||||
|
|
||||||
if !retCodeValue.Elem().IsValid() || retCodeValue.Type().String() != "*int" {
|
if retCodeValue.IsValid() && retCodeValue.Type().String() == "*int" &&
|
||||||
return fmt.Errorf("can not get retcode")
|
retCodeValue.Elem().IsValid() {
|
||||||
}
|
if retCodeValue.Elem().Int() == 0 {
|
||||||
|
return nil
|
||||||
if retCodeValue.Elem().Int() != 0 {
|
|
||||||
if !messageValue.Elem().IsValid() || messageValue.Type().String() != "*string" {
|
|
||||||
return fmt.Errorf("can not get error message")
|
|
||||||
}
|
}
|
||||||
|
err := &errors.QingCloudError{
|
||||||
return &errors.QingCloudError{
|
|
||||||
RetCode: int(retCodeValue.Elem().Int()),
|
RetCode: int(retCodeValue.Elem().Int()),
|
||||||
Message: messageValue.Elem().String(),
|
|
||||||
}
|
}
|
||||||
|
if messageValue.IsValid() && messageValue.Type().String() == "*string" {
|
||||||
|
if messageValue.Elem().IsValid() {
|
||||||
|
err.Message = messageValue.Elem().String()
|
||||||
|
} else {
|
||||||
|
err.Message = "null"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
return fmt.Errorf("invalid retCodeValue %v returned", retCodeValue)
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,3 +149,50 @@ func TestUnpacker_UnpackHTTPRequestWithError(t *testing.T) {
|
||||||
assert.Equal(t, e.RetCode, 1400)
|
assert.Equal(t, e.RetCode, 1400)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnpacker_UnpackHTTPRequestWithWrongType2(t *testing.T) {
|
||||||
|
type DescribeInstanceTypesOutput struct {
|
||||||
|
RetCode *int `json:"ret_code" name:"ret_code"`
|
||||||
|
Message *string `json:"message" name:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
|
httpResponse := &http.Response{Header: http.Header{}}
|
||||||
|
httpResponse.StatusCode = 200
|
||||||
|
httpResponse.Header.Set("Content-Type", "application/json")
|
||||||
|
responseString := `{
|
||||||
|
"message": null,
|
||||||
|
"ret_code":1400
|
||||||
|
}`
|
||||||
|
httpResponse.Body = ioutil.NopCloser(bytes.NewReader([]byte(responseString)))
|
||||||
|
|
||||||
|
output := &DescribeInstanceTypesOutput{}
|
||||||
|
outputValue := reflect.ValueOf(output)
|
||||||
|
unpacker := Unpacker{}
|
||||||
|
err := unpacker.UnpackHTTPRequest(&data.Operation{}, httpResponse, &outputValue)
|
||||||
|
if !assert.NotNil(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
println("err", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnpacker_UnpackHTTPRequestWithWrongHTTPStatus(t *testing.T) {
|
||||||
|
type DescribeInstanceTypesOutput struct {
|
||||||
|
RetCode *int `json:"ret_code" name:"ret_code"`
|
||||||
|
Message *string `json:"message" name:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
|
httpResponse := &http.Response{Header: http.Header{}}
|
||||||
|
httpResponse.StatusCode = 500
|
||||||
|
httpResponse.Header.Set("Content-Type", "application/json")
|
||||||
|
responseString := "{}"
|
||||||
|
httpResponse.Body = ioutil.NopCloser(bytes.NewReader([]byte(responseString)))
|
||||||
|
|
||||||
|
output := &DescribeInstanceTypesOutput{}
|
||||||
|
outputValue := reflect.ValueOf(output)
|
||||||
|
unpacker := Unpacker{}
|
||||||
|
err := unpacker.UnpackHTTPRequest(&data.Operation{}, httpResponse, &outputValue)
|
||||||
|
if !assert.NotNil(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
println("err", err.Error())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue