Repository: uber-go/zap
File: http_handler_test.go (Go)

Code before:
		},
		"PUT JSON unspecified": {
			Method:       http.MethodPut,
			ExpectedCode: http.StatusBadRequest,
			Body:         `{}`,
		},
		"PUT URL encoded unspecified": {
			Method:       http.MethodPut,
			ExpectedCode: http.StatusBadRequest,
			ContentType:  "application/x-www-form-urlencoded",
			Body:         "",
		},
		"POST JSON": {
			Method:       http.MethodPost,
			ExpectedCode: http.StatusMethodNotAllowed,
			Body:         `{"level":"warn"}`,
		},
		"POST URL": {
			Method:       http.MethodPost,
			ExpectedCode: http.StatusMethodNotAllowed,
			ContentType:  "application/x-www-form-urlencoded",
			Body:         "level=warn",
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			lvl := zap.NewAtomicLevel()
			lvl.SetLevel(zapcore.InfoLevel)

			ts := httptest.NewServer(lvl)
			defer ts.Close()

			req, err := http.NewRequest(test.Method, ts.URL, strings.NewReader(test.Body))
			require.NoError(t, err, "Error constructing %s request.", req.Method)
			if test.ContentType != "" {
				req.Header.Set("Content-Type", test.ContentType)
			}

			res, err := http.DefaultClient.Do(req)
			require.NoError(t, err, "Error making %s request.", req.Method)
			defer res.Body.Close()

			require.Equal(t, test.ExpectedCode, res.StatusCode, "Unexpected status code.")
			if test.ExpectedCode != http.StatusOK {
				// Don't need to test exact error message, but one should be present.
				var pld struct {
					Error string `json:"error"`
				}
				require.NoError(t, json.NewDecoder(res.Body).Decode(&pld), "Decoding response body")
				assert.NotEmpty(t, pld.Error, "Expected an error message")

Review comment (suggestion):
For table tests, consider renaming the test cases to `tt`.

  for _, tt := range tests {

Code after:
		},
		{
			desc:          "JSON ignores query",
			method:        http.MethodPut,
			query:         "?level=info",
			expectedCode:  http.StatusOK,
			expectedLevel: zap.WarnLevel,
			body:          `{"level":"warn"}`,
		},
		{
			desc:         "PUT JSON unrecognized",
			method:       http.MethodPut,
			expectedCode: http.StatusBadRequest,
			body:         `{"level":"unrecognized"}`,
		},
		{
			desc:         "PUT URL encoded unrecognized",
			method:       http.MethodPut,
			expectedCode: http.StatusBadRequest,
			contentType:  "application/x-www-form-urlencoded",
			body:         "level=unrecognized",
		},
		{
			desc:         "PUT JSON malformed",
			method:       http.MethodPut,
			expectedCode: http.StatusBadRequest,
			body:         `{"level":"warn`,
		},
		{
			desc:         "PUT URL encoded malformed",
			method:       http.MethodPut,
			query:        "?level=%",
			expectedCode: http.StatusBadRequest,
			contentType:  "application/x-www-form-urlencoded",
		},
		{
			desc:         "PUT Query parameters malformed",
			method:       http.MethodPut,
			expectedCode: http.StatusBadRequest,
			contentType:  "application/x-www-form-urlencoded",
			body:         "level=%",
		},
		{
			desc:         "PUT JSON unspecified",
			method:       http.MethodPut,
			expectedCode: http.StatusBadRequest,
			body:         `{}`,
		},
		{
			desc:         "PUT URL encoded unspecified",
			method:       http.MethodPut,