-
Notifications
You must be signed in to change notification settings - Fork 207
/
fmt_test.go
157 lines (153 loc) · 4.38 KB
/
fmt_test.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestHandleFmt(t *testing.T) {
s, err := newServer(testingOptions(t))
if err != nil {
t.Fatalf("newServer(testingOptions(t)): %v", err)
}
for _, tt := range []struct {
name string
method string
body string
imports bool
want string
wantErr string
}{
{
name: "OPTIONS no-op",
method: http.MethodOptions,
},
{
name: "classic",
method: http.MethodPost,
body: " package main\n func main( ) { }\n",
want: "package main\n\nfunc main() {}\n",
},
{
name: "classic_goimports",
method: http.MethodPost,
body: " package main\nvar _ = fmt.Printf",
imports: true,
want: "package main\n\nimport \"fmt\"\n\nvar _ = fmt.Printf\n",
},
{
name: "single_go_with_header",
method: http.MethodPost,
body: "-- prog.go --\n package main",
want: "-- prog.go --\npackage main\n",
},
{
name: "multi_go_with_header",
method: http.MethodPost,
body: "-- prog.go --\n package main\n\n\n-- two.go --\n package main\n var X = 5",
want: "-- prog.go --\npackage main\n-- two.go --\npackage main\n\nvar X = 5\n",
},
{
name: "multi_go_without_header",
method: http.MethodPost,
body: " package main\n\n\n-- two.go --\n package main\n var X = 5",
want: "package main\n-- two.go --\npackage main\n\nvar X = 5\n",
},
{
name: "single_go.mod_with_header",
method: http.MethodPost,
body: "-- go.mod --\n module \"foo\" ",
want: "-- go.mod --\nmodule foo\n",
},
{
name: "multi_go.mod_with_header",
method: http.MethodPost,
body: "-- a/go.mod --\n module foo\n\n\n-- b/go.mod --\n module \"bar\"",
want: "-- a/go.mod --\nmodule foo\n-- b/go.mod --\nmodule bar\n",
},
{
name: "only_format_go_and_go.mod",
method: http.MethodPost,
body: " package main \n\n\n" +
"-- go.mod --\n module foo \n\n\n" +
"-- plain.txt --\n plain text \n\n\n",
want: "package main\n-- go.mod --\nmodule foo\n-- plain.txt --\n plain text \n\n\n",
},
{
name: "error_gofmt",
method: http.MethodPost,
body: "package 123\n",
wantErr: "prog.go:1:9: expected 'IDENT', found 123",
},
{
name: "error_gofmt_with_header",
method: http.MethodPost,
body: "-- dir/one.go --\npackage 123\n",
wantErr: "dir/one.go:1:9: expected 'IDENT', found 123",
},
{
name: "error_goimports",
method: http.MethodPost,
body: "package 123\n",
imports: true,
wantErr: "prog.go:1:9: expected 'IDENT', found 123",
},
{
name: "error_goimports_with_header",
method: http.MethodPost,
body: "-- dir/one.go --\npackage 123\n",
imports: true,
wantErr: "dir/one.go:1:9: expected 'IDENT', found 123",
},
{
name: "error_go.mod",
method: http.MethodPost,
body: "-- go.mod --\n123\n",
wantErr: "go.mod:1: unknown directive: 123",
},
{
name: "error_go.mod_with_header",
method: http.MethodPost,
body: "-- dir/go.mod --\n123\n",
wantErr: "dir/go.mod:1: unknown directive: 123",
},
} {
t.Run(tt.name, func(t *testing.T) {
rec := httptest.NewRecorder()
form := url.Values{}
form.Set("body", tt.body)
if tt.imports {
form.Set("imports", "true")
}
req := httptest.NewRequest("POST", "/fmt", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
s.handleFmt(rec, req)
resp := rec.Result()
if resp.StatusCode != 200 {
t.Fatalf("code = %v", resp.Status)
}
corsHeader := "Access-Control-Allow-Origin"
if got, want := resp.Header.Get(corsHeader), "*"; got != want {
t.Errorf("Header %q: got %q; want %q", corsHeader, got, want)
}
if ct := resp.Header.Get("Content-Type"); ct != "application/json" {
t.Fatalf("Content-Type = %q; want application/json", ct)
}
var got fmtResponse
if err := json.NewDecoder(resp.Body).Decode(&got); err != nil {
t.Fatal(err)
}
if got.Body != tt.want {
t.Errorf("wrong output\n got: %q\nwant: %q\n", got.Body, tt.want)
}
if got.Error != tt.wantErr {
t.Errorf("wrong error\n got err: %q\nwant err: %q\n", got.Error, tt.wantErr)
}
})
}
}