github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/api/handlers/utils/handler_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/gorilla/mux"
    11  )
    12  
    13  func TestSupportedVersion(t *testing.T) {
    14  	req, err := http.NewRequest("GET",
    15  		fmt.Sprintf("/v%s/libpod/testing/versions", APIVersion[LibpodTree][CurrentAPIVersion]),
    16  		nil)
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	req = mux.SetURLVars(req, map[string]string{"version": APIVersion[LibpodTree][CurrentAPIVersion].String()})
    21  
    22  	rr := httptest.NewRecorder()
    23  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    24  		_, err := SupportedVersionWithDefaults(r)
    25  		switch {
    26  		case errors.Is(err, ErrVersionNotGiven): // for compat endpoints version optional
    27  			w.WriteHeader(http.StatusInternalServerError)
    28  			fmt.Fprint(w, err.Error())
    29  		case errors.Is(err, ErrVersionNotSupported): // version given but not supported
    30  			w.WriteHeader(http.StatusBadRequest)
    31  			fmt.Fprint(w, err.Error())
    32  		case err != nil:
    33  			w.WriteHeader(http.StatusInternalServerError)
    34  			fmt.Fprint(w, err.Error())
    35  		default: // all good
    36  			w.WriteHeader(http.StatusOK)
    37  			fmt.Fprint(w, "OK")
    38  		}
    39  	})
    40  	handler.ServeHTTP(rr, req)
    41  
    42  	if status := rr.Code; status != http.StatusOK {
    43  		t.Errorf("handler returned wrong status code: got %v want %v",
    44  			status, http.StatusOK)
    45  	}
    46  
    47  	// Check the response body is what we expect.
    48  	expected := `OK`
    49  	if rr.Body.String() != expected {
    50  		t.Errorf("handler returned unexpected body: got %q want %q",
    51  			rr.Body.String(), expected)
    52  	}
    53  }
    54  
    55  func TestUnsupportedVersion(t *testing.T) {
    56  	version := "999.999.999"
    57  	req, err := http.NewRequest("GET",
    58  		fmt.Sprintf("/v%s/libpod/testing/versions", version),
    59  		nil)
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	req = mux.SetURLVars(req, map[string]string{"version": version})
    64  
    65  	rr := httptest.NewRecorder()
    66  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    67  		_, err := SupportedVersionWithDefaults(r)
    68  		switch {
    69  		case errors.Is(err, ErrVersionNotGiven): // for compat endpoints version optional
    70  			w.WriteHeader(http.StatusInternalServerError)
    71  			fmt.Fprint(w, err.Error())
    72  		case errors.Is(err, ErrVersionNotSupported): // version given but not supported
    73  			w.WriteHeader(http.StatusBadRequest)
    74  			fmt.Fprint(w, err.Error())
    75  		case err != nil:
    76  			w.WriteHeader(http.StatusInternalServerError)
    77  			fmt.Fprint(w, err.Error())
    78  		default: // all good
    79  			w.WriteHeader(http.StatusOK)
    80  			fmt.Fprint(w, "OK")
    81  		}
    82  	})
    83  	handler.ServeHTTP(rr, req)
    84  
    85  	if status := rr.Code; status != http.StatusBadRequest {
    86  		t.Errorf("handler returned wrong status code: got %v want %v",
    87  			status, http.StatusBadRequest)
    88  	}
    89  
    90  	// Check the response body is what we expect.
    91  	expected := ErrVersionNotSupported.Error()
    92  	if rr.Body.String() != expected {
    93  		t.Errorf("handler returned unexpected body: got %q want %q",
    94  			rr.Body.String(), expected)
    95  	}
    96  }
    97  
    98  func TestEqualVersion(t *testing.T) {
    99  	version := "1.30.0"
   100  	req, err := http.NewRequest("GET",
   101  		fmt.Sprintf("/v%s/libpod/testing/versions", version),
   102  		nil)
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	req = mux.SetURLVars(req, map[string]string{"version": version})
   107  
   108  	rr := httptest.NewRecorder()
   109  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   110  		_, err := SupportedVersion(r, "=="+version)
   111  		switch {
   112  		case errors.Is(err, ErrVersionNotGiven): // for compat endpoints version optional
   113  			w.WriteHeader(http.StatusInternalServerError)
   114  			fmt.Fprint(w, err.Error())
   115  		case errors.Is(err, ErrVersionNotSupported): // version given but not supported
   116  			w.WriteHeader(http.StatusBadRequest)
   117  			fmt.Fprint(w, err.Error())
   118  		case err != nil:
   119  			w.WriteHeader(http.StatusInternalServerError)
   120  			fmt.Fprint(w, err.Error())
   121  		default: // all good
   122  			w.WriteHeader(http.StatusOK)
   123  			fmt.Fprint(w, "OK")
   124  		}
   125  	})
   126  	handler.ServeHTTP(rr, req)
   127  
   128  	if status := rr.Code; status != http.StatusOK {
   129  		t.Errorf("handler returned wrong status code: got %v want %v",
   130  			status, http.StatusOK)
   131  	}
   132  
   133  	// Check the response body is what we expect.
   134  	expected := http.StatusText(http.StatusOK)
   135  	if rr.Body.String() != expected {
   136  		t.Errorf("handler returned unexpected body: got %q want %q",
   137  			rr.Body.String(), expected)
   138  	}
   139  }