golang.org/x/playground@v0.0.0-20230418134305-14ebe15bcd59/fmt_test.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"encoding/json"
     9  	"net/http"
    10  	"net/http/httptest"
    11  	"net/url"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  func TestHandleFmt(t *testing.T) {
    17  	s, err := newServer(testingOptions(t))
    18  	if err != nil {
    19  		t.Fatalf("newServer(testingOptions(t)): %v", err)
    20  	}
    21  
    22  	for _, tt := range []struct {
    23  		name    string
    24  		method  string
    25  		body    string
    26  		imports bool
    27  		want    string
    28  		wantErr string
    29  	}{
    30  		{
    31  			name:   "OPTIONS no-op",
    32  			method: http.MethodOptions,
    33  		},
    34  		{
    35  			name:   "classic",
    36  			method: http.MethodPost,
    37  			body:   " package main\n    func main( ) {  }\n",
    38  			want:   "package main\n\nfunc main() {}\n",
    39  		},
    40  		{
    41  			name:    "classic_goimports",
    42  			method:  http.MethodPost,
    43  			body:    " package main\nvar _ = fmt.Printf",
    44  			imports: true,
    45  			want:    "package main\n\nimport \"fmt\"\n\nvar _ = fmt.Printf\n",
    46  		},
    47  		{
    48  			name:   "single_go_with_header",
    49  			method: http.MethodPost,
    50  			body:   "-- prog.go --\n  package main",
    51  			want:   "-- prog.go --\npackage main\n",
    52  		},
    53  		{
    54  			name:   "multi_go_with_header",
    55  			method: http.MethodPost,
    56  			body:   "-- prog.go --\n  package main\n\n\n-- two.go --\n   package main\n  var X = 5",
    57  			want:   "-- prog.go --\npackage main\n-- two.go --\npackage main\n\nvar X = 5\n",
    58  		},
    59  		{
    60  			name:   "multi_go_without_header",
    61  			method: http.MethodPost,
    62  			body:   "    package main\n\n\n-- two.go --\n   package main\n  var X = 5",
    63  			want:   "package main\n-- two.go --\npackage main\n\nvar X = 5\n",
    64  		},
    65  		{
    66  			name:   "single_go.mod_with_header",
    67  			method: http.MethodPost,
    68  			body:   "-- go.mod --\n   module   \"foo\"   ",
    69  			want:   "-- go.mod --\nmodule foo\n",
    70  		},
    71  		{
    72  			name:   "multi_go.mod_with_header",
    73  			method: http.MethodPost,
    74  			body:   "-- a/go.mod --\n  module foo\n\n\n-- b/go.mod --\n   module  \"bar\"",
    75  			want:   "-- a/go.mod --\nmodule foo\n-- b/go.mod --\nmodule bar\n",
    76  		},
    77  		{
    78  			name:   "only_format_go_and_go.mod",
    79  			method: http.MethodPost,
    80  			body: "    package   main   \n\n\n" +
    81  				"-- go.mod --\n   module   foo   \n\n\n" +
    82  				"-- plain.txt --\n   plain   text   \n\n\n",
    83  			want: "package main\n-- go.mod --\nmodule foo\n-- plain.txt --\n   plain   text   \n\n\n",
    84  		},
    85  		{
    86  			name:    "error_gofmt",
    87  			method:  http.MethodPost,
    88  			body:    "package 123\n",
    89  			wantErr: "prog.go:1:9: expected 'IDENT', found 123",
    90  		},
    91  		{
    92  			name:    "error_gofmt_with_header",
    93  			method:  http.MethodPost,
    94  			body:    "-- dir/one.go --\npackage 123\n",
    95  			wantErr: "dir/one.go:1:9: expected 'IDENT', found 123",
    96  		},
    97  		{
    98  			name:    "error_goimports",
    99  			method:  http.MethodPost,
   100  			body:    "package 123\n",
   101  			imports: true,
   102  			wantErr: "prog.go:1:9: expected 'IDENT', found 123",
   103  		},
   104  		{
   105  			name:    "error_goimports_with_header",
   106  			method:  http.MethodPost,
   107  			body:    "-- dir/one.go --\npackage 123\n",
   108  			imports: true,
   109  			wantErr: "dir/one.go:1:9: expected 'IDENT', found 123",
   110  		},
   111  		{
   112  			name:    "error_go.mod",
   113  			method:  http.MethodPost,
   114  			body:    "-- go.mod --\n123\n",
   115  			wantErr: "go.mod:1: unknown directive: 123",
   116  		},
   117  		{
   118  			name:    "error_go.mod_with_header",
   119  			method:  http.MethodPost,
   120  			body:    "-- dir/go.mod --\n123\n",
   121  			wantErr: "dir/go.mod:1: unknown directive: 123",
   122  		},
   123  	} {
   124  		t.Run(tt.name, func(t *testing.T) {
   125  			rec := httptest.NewRecorder()
   126  			form := url.Values{}
   127  			form.Set("body", tt.body)
   128  			if tt.imports {
   129  				form.Set("imports", "true")
   130  			}
   131  			req := httptest.NewRequest("POST", "/fmt", strings.NewReader(form.Encode()))
   132  			req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
   133  			s.handleFmt(rec, req)
   134  			resp := rec.Result()
   135  			if resp.StatusCode != 200 {
   136  				t.Fatalf("code = %v", resp.Status)
   137  			}
   138  			corsHeader := "Access-Control-Allow-Origin"
   139  			if got, want := resp.Header.Get(corsHeader), "*"; got != want {
   140  				t.Errorf("Header %q: got %q; want %q", corsHeader, got, want)
   141  			}
   142  			if ct := resp.Header.Get("Content-Type"); ct != "application/json" {
   143  				t.Fatalf("Content-Type = %q; want application/json", ct)
   144  			}
   145  			var got fmtResponse
   146  			if err := json.NewDecoder(resp.Body).Decode(&got); err != nil {
   147  				t.Fatal(err)
   148  			}
   149  			if got.Body != tt.want {
   150  				t.Errorf("wrong output\n got: %q\nwant: %q\n", got.Body, tt.want)
   151  			}
   152  			if got.Error != tt.wantErr {
   153  				t.Errorf("wrong error\n got err: %q\nwant err: %q\n", got.Error, tt.wantErr)
   154  			}
   155  		})
   156  	}
   157  }