-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ErrorWriter IsSupportedCheck with required connect protocol option (
#700) This PR fixes ErrorWriter to correctly return unsupported protocol if the option `WithRequireConnectProtocolHeader` is set and the header or query value isn't include in the request. It will now correctly return unsupported to ensure fallback options can process the error. Fixes #699
- Loading branch information
1 parent
32b3f43
commit 6fab35e
Showing
6 changed files
with
94 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2021-2024 The Connect Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package connect | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"connectrpc.com/connect/internal/assert" | ||
) | ||
|
||
func TestErrorWriter(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("RequireConnectProtocolHeader", func(t *testing.T) { | ||
t.Parallel() | ||
writer := NewErrorWriter(WithRequireConnectProtocolHeader()) | ||
|
||
t.Run("Unary", func(t *testing.T) { | ||
req := httptest.NewRequest(http.MethodPost, "http://localhost", nil) | ||
req.Header.Set("Content-Type", connectUnaryContentTypePrefix+codecNameJSON) | ||
assert.False(t, writer.IsSupported(req)) | ||
req.Header.Set(connectHeaderProtocolVersion, connectProtocolVersion) | ||
assert.True(t, writer.IsSupported(req)) | ||
}) | ||
t.Run("UnaryGET", func(t *testing.T) { | ||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil) | ||
assert.False(t, writer.IsSupported(req)) | ||
query := req.URL.Query() | ||
query.Set(connectUnaryConnectQueryParameter, connectUnaryConnectQueryValue) | ||
req.URL.RawQuery = query.Encode() | ||
assert.True(t, writer.IsSupported(req)) | ||
}) | ||
t.Run("Stream", func(t *testing.T) { | ||
req := httptest.NewRequest(http.MethodPost, "http://localhost", nil) | ||
req.Header.Set("Content-Type", connectStreamingContentTypePrefix+codecNameJSON) | ||
assert.True(t, writer.IsSupported(req)) // ignores WithRequireConnectProtocolHeader | ||
req.Header.Set(connectHeaderProtocolVersion, connectProtocolVersion) | ||
assert.True(t, writer.IsSupported(req)) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters