Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev committed Dec 18, 2024
1 parent b4e6306 commit ff48edd
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 60 deletions.
40 changes: 20 additions & 20 deletions private/buf/bufstudioagent/bufstudioagent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ func testPlainPostHandler(t *testing.T, upstreamServer *httptest.Server) {
defer agentServer.Close()

t.Run("content_type_grpc_proto", func(t *testing.T) {
requestProto := &studiov1alpha1.InvokeRequest{
requestProto := studiov1alpha1.InvokeRequest_builder{
Target: upstreamServer.URL + echoPath,
Headers: goHeadersToProtoHeaders(http.Header{
"Content-Type": []string{"application/grpc+proto"},
}),
Body: []byte("echothis"),
}
}.Build()
requestBytes := protoMarshalBase64(t, requestProto)
request, err := http.NewRequest(http.MethodPost, agentServer.URL, bytes.NewReader(requestBytes))
require.NoError(t, err)
Expand All @@ -97,21 +97,21 @@ func testPlainPostHandler(t *testing.T, upstreamServer *httptest.Server) {
invokeResponse := &studiov1alpha1.InvokeResponse{}
protoUnmarshalBase64(t, responseBytes, invokeResponse)
upstreamResponseHeaders := make(http.Header)
addProtoHeadersToGoHeader(invokeResponse.Headers, upstreamResponseHeaders)
addProtoHeadersToGoHeader(invokeResponse.Trailers, upstreamResponseHeaders)
addProtoHeadersToGoHeader(invokeResponse.GetHeaders(), upstreamResponseHeaders)
addProtoHeadersToGoHeader(invokeResponse.GetTrailers(), upstreamResponseHeaders)
assert.Equal(t, "0", upstreamResponseHeaders.Get("grpc-status"))
assert.Equal(t, []byte("echo: echothis"), invokeResponse.Body)
assert.Equal(t, []byte("echo: echothis"), invokeResponse.GetBody())
assert.Equal(t, "foo-value", upstreamResponseHeaders.Get("Echo-Bar"))
})

t.Run("content_type_application_proto", func(t *testing.T) {
requestProto := &studiov1alpha1.InvokeRequest{
requestProto := studiov1alpha1.InvokeRequest_builder{
Target: upstreamServer.URL + echoPath,
Headers: goHeadersToProtoHeaders(http.Header{
"Content-Type": []string{"application/proto"},
}),
Body: []byte("echothis"),
}
}.Build()
requestBytes := protoMarshalBase64(t, requestProto)
request, err := http.NewRequest(http.MethodPost, agentServer.URL, bytes.NewReader(requestBytes))
require.NoError(t, err)
Expand All @@ -129,10 +129,10 @@ func testPlainPostHandler(t *testing.T, upstreamServer *httptest.Server) {
invokeResponse := &studiov1alpha1.InvokeResponse{}
protoUnmarshalBase64(t, responseBytes, invokeResponse)
upstreamResponseHeaders := make(http.Header)
addProtoHeadersToGoHeader(invokeResponse.Headers, upstreamResponseHeaders)
addProtoHeadersToGoHeader(invokeResponse.Trailers, upstreamResponseHeaders)
addProtoHeadersToGoHeader(invokeResponse.GetHeaders(), upstreamResponseHeaders)
addProtoHeadersToGoHeader(invokeResponse.GetTrailers(), upstreamResponseHeaders)
assert.Equal(t, "", upstreamResponseHeaders.Get("grpc-status"))
assert.Equal(t, []byte("echo: echothis"), invokeResponse.Body)
assert.Equal(t, []byte("echo: echothis"), invokeResponse.GetBody())
assert.Equal(t, "foo-value", upstreamResponseHeaders.Get("Echo-Bar"))
})
}
Expand All @@ -151,12 +151,12 @@ func testPlainPostHandlerErrors(t *testing.T, upstreamServer *httptest.Server) {
defer agentServer.Close()

t.Run("forbidden_header", func(t *testing.T) {
requestProto := &studiov1alpha1.InvokeRequest{
requestProto := studiov1alpha1.InvokeRequest_builder{
Target: upstreamServer.URL + echoPath,
Headers: goHeadersToProtoHeaders(http.Header{
"forbidden-header": []string{"<tokens>"},
}),
}
}.Build()
requestBytes := protoMarshalBase64(t, requestProto)
request, err := http.NewRequest(http.MethodPost, agentServer.URL, bytes.NewReader(requestBytes))
require.NoError(t, err)
Expand All @@ -168,13 +168,13 @@ func testPlainPostHandlerErrors(t *testing.T, upstreamServer *httptest.Server) {
})

t.Run("error_response", func(t *testing.T) {
requestProto := &studiov1alpha1.InvokeRequest{
requestProto := studiov1alpha1.InvokeRequest_builder{
Target: upstreamServer.URL + errorPath,
Headers: goHeadersToProtoHeaders(http.Header{
"Content-Type": []string{"application/grpc"},
}),
Body: []byte("something"),
}
}.Build()
requestBytes := protoMarshalBase64(t, requestProto)
request, err := http.NewRequest(http.MethodPost, agentServer.URL, bytes.NewReader(requestBytes))
require.NoError(t, err)
Expand All @@ -188,8 +188,8 @@ func testPlainPostHandlerErrors(t *testing.T, upstreamServer *httptest.Server) {
invokeResponse := &studiov1alpha1.InvokeResponse{}
protoUnmarshalBase64(t, responseBytes, invokeResponse)
upstreamResponseHeaders := make(http.Header)
addProtoHeadersToGoHeader(invokeResponse.Headers, upstreamResponseHeaders)
addProtoHeadersToGoHeader(invokeResponse.Trailers, upstreamResponseHeaders)
addProtoHeadersToGoHeader(invokeResponse.GetHeaders(), upstreamResponseHeaders)
addProtoHeadersToGoHeader(invokeResponse.GetTrailers(), upstreamResponseHeaders)
assert.Equal(t, strconv.Itoa(int(connect.CodeFailedPrecondition)), upstreamResponseHeaders.Get("grpc-status"))
assert.Equal(t, "something", upstreamResponseHeaders.Get("grpc-message"))
})
Expand Down Expand Up @@ -231,12 +231,12 @@ func testPlainPostHandlerErrors(t *testing.T, upstreamServer *httptest.Server) {
}
}()

requestProto := &studiov1alpha1.InvokeRequest{
requestProto := studiov1alpha1.InvokeRequest_builder{
Target: "http://" + listener.Addr().String(),
Headers: goHeadersToProtoHeaders(http.Header{
"Content-Type": []string{"application/grpc"},
}),
}
}.Build()
requestBytes := protoMarshalBase64(t, requestProto)
request, err := http.NewRequest(http.MethodPost, agentServer.URL, bytes.NewReader(requestBytes))
require.NoError(t, err)
Expand Down Expand Up @@ -305,8 +305,8 @@ func protoUnmarshalBase64(t *testing.T, base64Bytes []byte, message proto.Messag

func addProtoHeadersToGoHeader(fromHeaders []*studiov1alpha1.Headers, toHeaders http.Header) {
for _, meta := range fromHeaders {
for _, value := range meta.Value {
toHeaders.Add(meta.Key, value)
for _, value := range meta.GetValue() {
toHeaders.Add(meta.GetKey(), value)
}
}
}
4 changes: 2 additions & 2 deletions private/buf/cmd/buf/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1725,11 +1725,11 @@ func requireBuildOutputFilePaths(t *testing.T, expectedFilePathToInfo map[string

filesToCheck := slicesext.ToStructMap(slicesext.MapKeysToSlice(expectedFilePathToInfo))

for _, imageFile := range outputImage.File {
for _, imageFile := range outputImage.GetFile() {
filePath := imageFile.GetName()
expectedFileInfo, ok := expectedFilePathToInfo[filePath]
require.Truef(t, ok, "unexpected file in the image built: %s", filePath)
require.Equal(t, expectedFileInfo.isImport, imageFile.BufExtension.GetIsImport())
require.Equal(t, expectedFileInfo.isImport, imageFile.GetBufExtension().GetIsImport())
if expectedFileInfo.moduleFullName != "" {
moduleName := imageFile.GetBufExtension().GetModuleInfo().GetName()
require.NotNil(t, moduleName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func TestBasic(t *testing.T) {
newImage.Files(),
)

protoImage := &imagev1.Image{
protoImage := imagev1.Image_builder{
File: []*imagev1.ImageFile{
protoImageFileAA,
protoImageFileImport,
Expand All @@ -450,7 +450,7 @@ func TestBasic(t *testing.T) {
protoImageFileBB,
protoImageFileOutlandishDirectoryName,
},
}
}.Build()
newImage, err = bufimage.NewImageForProto(protoImage)
require.NoError(t, err)
AssertImageFilesEqual(
Expand Down Expand Up @@ -883,7 +883,7 @@ func TestImageFileInfosWithOnlyTargetsAndTargetImports(t *testing.T) {

func testProtoImageFileToFileDescriptorProto(imageFile *imagev1.ImageFile) *descriptorpb.FileDescriptorProto {
return &descriptorpb.FileDescriptorProto{
Name: imageFile.Name,
Dependency: imageFile.Dependency,
Name: proto.ValueOrNil(imageFile.HasName(), imageFile.GetName),
Dependency: imageFile.GetDependency(),
}
}
52 changes: 26 additions & 26 deletions private/bufpkg/bufimage/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import (

func TestCloneImage(t *testing.T) {
t.Parallel()
protoImage := &imagev1.Image{
protoImage := imagev1.Image_builder{
File: []*imagev1.ImageFile{
{
imagev1.ImageFile_builder{
Syntax: proto.String("proto3"),
Name: proto.String("a.proto"),
Dependency: []string{"b.proto", "c.proto"},
Expand All @@ -57,21 +57,21 @@ func TestCloneImage(t *testing.T) {
},
},
},
BufExtension: &imagev1.ImageFileExtension{
BufExtension: imagev1.ImageFileExtension_builder{
IsImport: proto.Bool(false),
IsSyntaxUnspecified: proto.Bool(false),
UnusedDependency: []int32{1},
ModuleInfo: &imagev1.ModuleInfo{
Name: &imagev1.ModuleName{
ModuleInfo: imagev1.ModuleInfo_builder{
Name: imagev1.ModuleName_builder{
Remote: proto.String("buf.build"),
Owner: proto.String("foo"),
Repository: proto.String("bar"),
},
}.Build(),
Commit: proto.String("9876543210fedcba9876543210fedcba"),
},
},
},
{
}.Build(),
}.Build(),
}.Build(),
imagev1.ImageFile_builder{
Syntax: proto.String("proto3"),
Name: proto.String("b.proto"),
Package: proto.String("abc.def"),
Expand All @@ -90,37 +90,37 @@ func TestCloneImage(t *testing.T) {
},
},
},
BufExtension: &imagev1.ImageFileExtension{
BufExtension: imagev1.ImageFileExtension_builder{
IsImport: proto.Bool(true),
IsSyntaxUnspecified: proto.Bool(false),
ModuleInfo: &imagev1.ModuleInfo{
Name: &imagev1.ModuleName{
ModuleInfo: imagev1.ModuleInfo_builder{
Name: imagev1.ModuleName_builder{
Remote: proto.String("buf.build"),
Owner: proto.String("foo"),
Repository: proto.String("baz"),
},
}.Build(),
Commit: proto.String("0123456789abcdef0123456789abcdef"),
},
},
},
{
}.Build(),
}.Build(),
}.Build(),
imagev1.ImageFile_builder{
Syntax: proto.String("proto2"),
Name: proto.String("c.proto"),
BufExtension: &imagev1.ImageFileExtension{
BufExtension: imagev1.ImageFileExtension_builder{
IsImport: proto.Bool(true),
IsSyntaxUnspecified: proto.Bool(true),
ModuleInfo: &imagev1.ModuleInfo{
Name: &imagev1.ModuleName{
ModuleInfo: imagev1.ModuleInfo_builder{
Name: imagev1.ModuleName_builder{
Remote: proto.String("buf.build"),
Owner: proto.String("foo"),
Repository: proto.String("baz"),
},
}.Build(),
Commit: proto.String("0123456789abcdef0123456789abcdef"),
},
},
},
}.Build(),
}.Build(),
}.Build(),
},
}
}.Build()

image, err := NewImageForProto(protoImage)
require.NoError(t, err)
Expand Down
16 changes: 8 additions & 8 deletions private/bufpkg/bufimage/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ import (

func TestStripBufExtensionField(t *testing.T) {
t.Parallel()
file := &imagev1.ImageFile{
BufExtension: &imagev1.ImageFileExtension{
file := imagev1.ImageFile_builder{
BufExtension: imagev1.ImageFileExtension_builder{
IsImport: proto.Bool(true),
UnusedDependency: []int32{1, 3, 5},
ModuleInfo: &imagev1.ModuleInfo{
Name: &imagev1.ModuleName{
ModuleInfo: imagev1.ModuleInfo_builder{
Name: imagev1.ModuleName_builder{
Remote: proto.String("buf.build"),
Owner: proto.String("foo"),
Repository: proto.String("bar"),
},
}.Build(),
Commit: proto.String("1234981234123412341234"),
},
},
}
}.Build(),
}.Build(),
}.Build()
dataToBeStripped, err := protoencoding.NewWireMarshaler().Marshal(file)
require.NoError(t, err)

Expand Down

0 comments on commit ff48edd

Please sign in to comment.