diff --git a/azurerm/response.go b/azurerm/response.go new file mode 100644 index 000000000000..e3b01100b50b --- /dev/null +++ b/azurerm/response.go @@ -0,0 +1,17 @@ +package azurerm + +import ( + "net/http" + + "github.com/Azure/go-autorest/autorest" +) + +func responseWasNotFound(resp autorest.Response) bool { + if r := resp.Response; r != nil { + if r.StatusCode == http.StatusNotFound { + return true + } + } + + return false +} diff --git a/azurerm/response_test.go b/azurerm/response_test.go new file mode 100644 index 000000000000..a884a53e0cd7 --- /dev/null +++ b/azurerm/response_test.go @@ -0,0 +1,39 @@ +package azurerm + +import ( + "net/http" + "testing" + + "github.com/Azure/go-autorest/autorest" +) + +func TestResponseNotFound_DroppedConnection(t *testing.T) { + resp := autorest.Response{} + if responseWasNotFound(resp) { + t.Fatalf("responseWasNotFound should return `false` for a dropped connection") + } +} + +func TestResponseNotFound_StatusCodes(t *testing.T) { + testCases := []struct { + statusCode int + expectedResult bool + }{ + { http.StatusOK, false, }, + { http.StatusInternalServerError, false }, + { http.StatusNotFound, true }, + } + + for _, test := range testCases { + resp := autorest.Response{ + Response: &http.Response{ + StatusCode: test.statusCode, + }, + } + result := responseWasNotFound(resp) + if test.expectedResult != result { + t.Fatalf("Expected '%+v' for status code '%d' - got '%+v'", + test.expectedResult, test.statusCode, result) + } + } +} \ No newline at end of file