github.com/minio/minio-go/v6@v6.0.57/test-utils_test.go (about) 1 /* 2 * MinIO Go Library for Amazon S3 Compatible Cloud Storage 3 * Copyright 2015-2017 MinIO, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package minio 19 20 import ( 21 "bytes" 22 "encoding/xml" 23 "io/ioutil" 24 "net/http" 25 "strconv" 26 ) 27 28 // Contains common used utilities for tests. 29 30 // APIError Used for mocking error response from server. 31 type APIError struct { 32 Code string 33 Description string 34 HTTPStatusCode int 35 } 36 37 // Mocks XML error response from the server. 38 func generateErrorResponse(resp *http.Response, APIErr APIError, bucketName string) *http.Response { 39 // generate error response. 40 errorResponse := getAPIErrorResponse(APIErr, bucketName) 41 encodedErrorResponse := encodeResponse(errorResponse) 42 // write Header. 43 resp.StatusCode = APIErr.HTTPStatusCode 44 resp.Body = ioutil.NopCloser(bytes.NewBuffer(encodedErrorResponse)) 45 46 return resp 47 } 48 49 // getErrorResponse gets in standard error and resource value and 50 // provides a encodable populated response values. 51 func getAPIErrorResponse(err APIError, bucketName string) ErrorResponse { 52 var errResp = ErrorResponse{} 53 errResp.Code = err.Code 54 errResp.Message = err.Description 55 errResp.BucketName = bucketName 56 return errResp 57 } 58 59 // Encodes the response headers into XML format. 60 func encodeResponse(response interface{}) []byte { 61 var bytesBuffer bytes.Buffer 62 bytesBuffer.WriteString(xml.Header) 63 encode := xml.NewEncoder(&bytesBuffer) 64 encode.Encode(response) 65 return bytesBuffer.Bytes() 66 } 67 68 // Convert string to bool and always return false if any error 69 func mustParseBool(str string) bool { 70 b, err := strconv.ParseBool(str) 71 if err != nil { 72 return false 73 } 74 return b 75 }