1
2
3
4
5
6
7
8
9
10
11
12
13
14 package expfmt
15
16 import (
17 "testing"
18
19 "github.com/prometheus/common/model"
20 )
21
22
23
24
25 func TestToFormatType(t *testing.T) {
26 tests := []struct {
27 format Format
28 expected FormatType
29 }{
30 {
31 format: fmtProtoCompact,
32 expected: TypeProtoCompact,
33 },
34 {
35 format: fmtProtoDelim,
36 expected: TypeProtoDelim,
37 },
38 {
39 format: fmtProtoText,
40 expected: TypeProtoText,
41 },
42 {
43 format: fmtOpenMetrics_1_0_0,
44 expected: TypeOpenMetrics,
45 },
46 {
47 format: fmtText,
48 expected: TypeTextPlain,
49 },
50 {
51 format: fmtOpenMetrics_0_0_1,
52 expected: TypeOpenMetrics,
53 },
54 {
55 format: "application/vnd.google.protobuf; proto=BadProtocol; encoding=text",
56 expected: TypeUnknown,
57 },
58 {
59 format: "application/vnd.google.protobuf",
60 expected: TypeUnknown,
61 },
62 {
63 format: "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily=bad",
64 expected: TypeUnknown,
65 },
66
67 {
68 format: "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily",
69 expected: TypeUnknown,
70 },
71
72 {
73 format: "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=textual",
74 expected: TypeUnknown,
75 },
76
77 {
78 format: "application/openmetrics-text; version=1.0.0; charset=ascii",
79 expected: TypeUnknown,
80 },
81 {
82 format: "text/plain",
83 expected: TypeTextPlain,
84 },
85 {
86 format: "text/plain; version=invalid",
87 expected: TypeUnknown,
88 },
89 {
90 format: "gobbledygook",
91 expected: TypeUnknown,
92 },
93 }
94 for _, test := range tests {
95 if test.format.FormatType() != test.expected {
96 t.Errorf("expected %v got %v", test.expected, test.format.FormatType())
97 }
98 }
99 }
100
101 func TestToEscapingScheme(t *testing.T) {
102 tests := []struct {
103 format Format
104 expected model.EscapingScheme
105 }{
106 {
107 format: fmtProtoCompact,
108 expected: model.ValueEncodingEscaping,
109 },
110 {
111 format: "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=underscores",
112 expected: model.UnderscoreEscaping,
113 },
114 {
115 format: "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=allow-utf-8",
116 expected: model.NoEscaping,
117 },
118
119 {
120 format: "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=invalid",
121 expected: model.NameEscapingScheme,
122 },
123 }
124 for _, test := range tests {
125 if test.format.ToEscapingScheme() != test.expected {
126 t.Errorf("expected %v got %v", test.expected, test.format.ToEscapingScheme())
127 }
128 }
129 }
130
View as plain text