Skip to content

Commit

Permalink
modify read err from header unit testing (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzong18 authored and huiguangjun committed Mar 23, 2023
1 parent 64b34dc commit 95400a1
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 4 deletions.
140 changes: 139 additions & 1 deletion oss/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4905,10 +4905,148 @@ func (s *OssClientSuite) TestExtendHttpResponseStatusCode(c *C) {
_, err = bucket.GetObject("unkown-error")
serviceErr, isSuc = err.(ServiceError)
c.Assert(isSuc, Equals, false)
prefix := "unkown response body, status = 509"
prefix := "unknown response body, status = 509"
ok := strings.Contains(err.Error(), prefix)
c.Assert(ok, Equals, true)
svr.Close()
}

func emptyBodyEcHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set(HTTPHeaderOssRequestID, "123456")
w.Header().Set(HTTPHeaderOssEc, "0001-00000309")
w.WriteHeader(309)
fmt.Fprintf(w, "")
}

func emptyBodyHeaderHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set(HTTPHeaderOssRequestID, "64195A905C006935335FE181")
w.Header().Set(HTTPHeaderOssEc, "0026-00000001")
w.Header().Set(HTTPHeaderOssErr, "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPEVycm9yPgogIDxDb2RlPk5vU3VjaEtleTwvQ29kZT4KICA8TWVzc2FnZT5UaGUgc3BlY2lmaWVkIGtleSBkb2VzIG5vdCBleGlzdC48L01lc3NhZ2U+CiAgPFJlcXVlc3RJZD42NDE5NUE5MDVDMDA2OTM1MzM1RkUxODE8L1JlcXVlc3RJZD4KICA8SG9zdElkPmRlbW8td2Fsa2VyLTY5NjEub3NzLWNuLWhhbmd6aG91LmFsaXl1bmNzLmNvbTwvSG9zdElkPgogIDxLZXk+ZGVtby0xMTEudXh1PC9LZXk+CiAgPEVDPjAwMjYtMDAwMDAwMDE8L0VDPgo8L0Vycm9yPgo=")
w.WriteHeader(404)
fmt.Fprintf(w, "")
}

func serviceErrorBodyEcHandler(w http.ResponseWriter, r *http.Request) {
err := ServiceError{
Code: "510",
Message: "service response error",
RequestID: "ABCDEF",
HostID: "127.0.0.1",
Endpoint: "127.0.0.1",
StatusCode: 510,
Ec: "0001-00000510",
}
data, _ := xml.MarshalIndent(&err, "", " ")
w.Header().Set(HTTPHeaderOssRequestID, "ABCDEF")
w.WriteHeader(510)
fmt.Fprintf(w, string(data))
}
func serviceErrorBodyEmptyEndpiointEcHandler(w http.ResponseWriter, r *http.Request) {
err := ServiceError{
Code: "510",
Message: "service response error",
RequestID: "ABCDEF",
HostID: "127.0.0.1",
Endpoint: "",
StatusCode: 510,
Ec: "0001-00000510",
}
data, _ := xml.MarshalIndent(&err, "", " ")
w.Header().Set(HTTPHeaderOssRequestID, "ABCDEF")
w.WriteHeader(510)
fmt.Fprintf(w, string(data))
}

func unknownErrorBodyEcHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(509)
w.Header().Set(HTTPHeaderOssEc, "0001-00000509")
fmt.Fprintf(w, "unkown response error")
}

func (s *OssClientSuite) TestExtendHttpResponseStatusCodeWithEc(c *C) {
// get port
rand.Seed(time.Now().Unix())
port := 10000 + rand.Intn(10000)

// start http server
httpAddr := fmt.Sprintf("127.0.0.1:%d", port)
mux := http.NewServeMux()
mux.HandleFunc("/empty-body/empty-body", emptyBodyEcHandler)
mux.HandleFunc("/service-error/service-error", serviceErrorBodyEcHandler)
mux.HandleFunc("/service-error/service-error-point", serviceErrorBodyEmptyEndpiointEcHandler)
mux.HandleFunc("/unknown-error/unknown-error", unknownErrorBodyEcHandler)

mux.HandleFunc("/empty-body/read-err-from-header", emptyBodyHeaderHandler)
svr := &http.Server{
Addr: httpAddr,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
Handler: mux,
}

go func() {
svr.ListenAndServe()
}()

time.Sleep(5 * time.Second)

testEndpoint := httpAddr
client, err := New(testEndpoint, accessID, accessKey)

bucket, err := client.Bucket("empty-body")
_, err = bucket.GetObject("empty-body")
serviceErr, isSuc := err.(ServiceError)
testLogger.Println(serviceErr)
c.Assert(isSuc, Equals, true)
c.Assert(serviceErr.StatusCode, Equals, 309)
c.Assert(serviceErr.RequestID, Equals, "123456")
c.Assert(serviceErr.Ec, Equals, "0001-00000309")
prefix := "oss: service returned error: StatusCode=309, ErrorCode=, ErrorMessage=\"\", RequestId=123456, Ec=0001-00000309"
ok := strings.Contains(err.Error(), prefix)
c.Assert(ok, Equals, true)

bucket, err = client.Bucket("empty-body")
_, err = bucket.GetObject("read-err-from-header")
serviceErr, isSuc = err.(ServiceError)
c.Assert(isSuc, Equals, true)
c.Assert(serviceErr.StatusCode, Equals, 404)
c.Assert(serviceErr.RequestID, Equals, "64195A905C006935335FE181")
c.Assert(serviceErr.Ec, Equals, "0026-00000001")

prefix = "oss: service returned error: StatusCode=404, ErrorCode=NoSuchKey, ErrorMessage=\"The specified key does not exist.\", RequestId=64195A905C006935335FE181, Ec=0026-00000001"
c.Assert(err.Error(), Equals, prefix)

bucket, err = client.Bucket("service-error")
_, err = bucket.GetObject("service-error")
serviceErr, isSuc = (err).(ServiceError)
c.Assert(isSuc, Equals, true)
c.Assert(serviceErr.StatusCode, Equals, 510)
c.Assert(serviceErr.RequestID, Equals, "ABCDEF")
c.Assert(serviceErr.Ec, Equals, "0001-00000510")
prefix = "oss: service returned error: StatusCode=510, ErrorCode=510, ErrorMessage=\"service response error\", RequestId=ABCDEF, Endpoint=127.0.0.1, Ec=0001-00000510"
ok = strings.Contains(err.Error(), prefix)
c.Assert(ok, Equals, true)

bucket, err = client.Bucket("service-error")
_, err = bucket.GetObject("service-error-point")
serviceErr, isSuc = (err).(ServiceError)
c.Assert(isSuc, Equals, true)
c.Assert(serviceErr.StatusCode, Equals, 510)
c.Assert(serviceErr.RequestID, Equals, "ABCDEF")
c.Assert(serviceErr.Ec, Equals, "0001-00000510")
prefix = "oss: service returned error: StatusCode=510, ErrorCode=510, ErrorMessage=\"service response error\", RequestId=ABCDEF, Ec=0001-00000510"
ok = strings.Contains(err.Error(), prefix)
c.Assert(ok, Equals, true)

bucket, err = client.Bucket("unknown-error")
_, err = bucket.GetObject("unknown-error")
serviceErr, isSuc = err.(ServiceError)
c.Assert(serviceErr.Ec, Equals, "")
testLogger.Println(err.Error())
c.Assert(isSuc, Equals, false)
prefix = "unknown response body, status = 509 status code 509, RequestId = "
c.Assert(err.Error(), Equals, prefix)
svr.Close()
}

Expand Down
6 changes: 3 additions & 3 deletions oss/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var signKeyList = []string{"acl", "uploads", "location", "cors",
"x-oss-enable-md5", "x-oss-enable-sha1", "x-oss-enable-sha256",
"x-oss-hash-ctx", "x-oss-md5-ctx", "transferAcceleration",
"regionList", "cloudboxes", "x-oss-ac-source-ip", "x-oss-ac-subnet-mask", "x-oss-ac-vpc-id", "x-oss-ac-forward-allow",
"metaQuery", "resourceGroup","rtc",
"metaQuery", "resourceGroup", "rtc",
}

// init initializes Conn
Expand Down Expand Up @@ -577,9 +577,9 @@ func (conn Conn) handleResponse(resp *http.Response, crc hash.Hash64) (*Response
resp.Header.Get(HTTPHeaderOssRequestID))
if errIn != nil { // error unmarshal the error response
if len(resp.Header.Get(HTTPHeaderOssEc)) > 0 {
err = fmt.Errorf("oss: service returned invalid response body, status = %s, RequestId = %s, ec = %s", resp.Status, resp.Header.Get(HTTPHeaderOssRequestID), resp.Header.Get(HTTPHeaderOssEc))
err = fmt.Errorf("unknown response body, status = %s, RequestId = %s, ec = %s", resp.Status, resp.Header.Get(HTTPHeaderOssRequestID), resp.Header.Get(HTTPHeaderOssEc))
} else {
err = fmt.Errorf("oss: service returned invalid response body, status = %s, RequestId = %s", resp.Status, resp.Header.Get(HTTPHeaderOssRequestID))
err = fmt.Errorf("unknown response body, status = %s, RequestId = %s", resp.Status, resp.Header.Get(HTTPHeaderOssRequestID))
}
} else {
err = srvErr
Expand Down

0 comments on commit 95400a1

Please sign in to comment.