File: marshal_httpbodyproto_test.go

package info (click to toggle)
golang-github-grpc-ecosystem-grpc-gateway.v2 2.11.3-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,148 kB
  • sloc: javascript: 352; makefile: 136; sh: 26
file content (54 lines) | stat: -rw-r--r-- 1,218 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package runtime_test

import (
	"bytes"
	"testing"

	"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
	"google.golang.org/genproto/googleapis/api/httpbody"
	"google.golang.org/protobuf/encoding/protojson"
)

func TestHTTPBodyContentType(t *testing.T) {
	m := runtime.HTTPBodyMarshaler{
		&runtime.JSONPb{
			MarshalOptions: protojson.MarshalOptions{
				UseProtoNames: true,
			},
		},
	}
	expected := "CustomContentType"
	message := &httpbody.HttpBody{
		ContentType: expected,
	}
	res := m.ContentType(nil)
	if res != "application/json" {
		t.Errorf("content type not equal (%q, %q)", res, expected)
	}
	res = m.ContentType(message)
	if res != expected {
		t.Errorf("content type not equal (%q, %q)", res, expected)
	}
}

func TestHTTPBodyMarshal(t *testing.T) {
	m := runtime.HTTPBodyMarshaler{
		&runtime.JSONPb{
			MarshalOptions: protojson.MarshalOptions{
				UseProtoNames: true,
			},
		},
	}
	expected := []byte("Some test")
	message := &httpbody.HttpBody{
		Data: expected,
	}
	res, err := m.Marshal(message)
	if err != nil {
		t.Errorf("m.Marshal(%#v) failed with %v; want success", message, err)
	}
	if !bytes.Equal(res, expected) {
		t.Errorf("Marshalled data not equal (%q, %q)", res, expected)

	}
}