ExecObjectLayerAPIAnonTest - Helper function to validate object Layer API handler response for anonymous/unsigned and unknown signature type HTTP request. Here is the brief description of some of the arguments to the function below. apiRouter - http.Handler with the relevant API endPoint (API endP
(t *testing.T, obj ObjectLayer, testName, bucketName, objectName, instanceType string, apiRouter http.Handler, anonReq *http.Request, bucketPolicy *policy.Policy, )
| 1596 | // |
| 1597 | // STEP 1: Call the handler with the unsigned HTTP request (anonReq), assert for the `ErrAccessDenied` error response. |
| 1598 | func ExecObjectLayerAPIAnonTest(t *testing.T, obj ObjectLayer, testName, bucketName, objectName, instanceType string, apiRouter http.Handler, |
| 1599 | anonReq *http.Request, bucketPolicy *policy.Policy, |
| 1600 | ) { |
| 1601 | anonTestStr := "Anonymous HTTP request test" |
| 1602 | unknownSignTestStr := "Unknown HTTP signature test" |
| 1603 | |
| 1604 | // simple function which returns a message which gives the context of the test |
| 1605 | // and then followed by the actual error message. |
| 1606 | failTestStr := func(testType, failMsg string) string { |
| 1607 | return fmt.Sprintf("MinIO %s: %s fail for \"%s\": \n<Error> %s", instanceType, testType, testName, failMsg) |
| 1608 | } |
| 1609 | |
| 1610 | // httptest Recorder to capture all the response by the http handler. |
| 1611 | rec := httptest.NewRecorder() |
| 1612 | // reading the body to preserve it so that it can be used again for second attempt of sending unsigned HTTP request. |
| 1613 | // If the body is read in the handler the same request cannot be made use of. |
| 1614 | buf, err := io.ReadAll(anonReq.Body) |
| 1615 | if err != nil { |
| 1616 | t.Fatal(failTestStr(anonTestStr, err.Error())) |
| 1617 | } |
| 1618 | |
| 1619 | // creating 2 read closer (to set as request body) from the body content. |
| 1620 | readerOne := io.NopCloser(bytes.NewBuffer(buf)) |
| 1621 | readerTwo := io.NopCloser(bytes.NewBuffer(buf)) |
| 1622 | |
| 1623 | anonReq.Body = readerOne |
| 1624 | |
| 1625 | // call the HTTP handler. |
| 1626 | apiRouter.ServeHTTP(rec, anonReq) |
| 1627 | |
| 1628 | // expected error response when the unsigned HTTP request is not permitted. |
| 1629 | accessDenied := getAPIError(ErrAccessDenied).HTTPStatusCode |
| 1630 | if rec.Code != accessDenied { |
| 1631 | t.Fatal(failTestStr(anonTestStr, fmt.Sprintf("Object API Nil Test expected to fail with %d, but failed with %d", accessDenied, rec.Code))) |
| 1632 | } |
| 1633 | |
| 1634 | // HEAD HTTTP request doesn't contain response body. |
| 1635 | if anonReq.Method != http.MethodHead { |
| 1636 | // read the response body. |
| 1637 | var actualContent []byte |
| 1638 | actualContent, err = io.ReadAll(rec.Body) |
| 1639 | if err != nil { |
| 1640 | t.Fatal(failTestStr(anonTestStr, fmt.Sprintf("Failed parsing response body: <ERROR> %v", err))) |
| 1641 | } |
| 1642 | |
| 1643 | actualError := &APIErrorResponse{} |
| 1644 | if err = xml.Unmarshal(actualContent, actualError); err != nil { |
| 1645 | t.Fatal(failTestStr(anonTestStr, "error response failed to parse error XML")) |
| 1646 | } |
| 1647 | |
| 1648 | if actualError.BucketName != bucketName { |
| 1649 | t.Fatal(failTestStr(anonTestStr, "error response bucket name differs from expected value")) |
| 1650 | } |
| 1651 | |
| 1652 | if actualError.Key != objectName { |
| 1653 | t.Fatal(failTestStr(anonTestStr, "error response object name differs from expected value")) |
| 1654 | } |
| 1655 | } |
no test coverage detected