github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/edge/pkg/servicebus/util/httpclient_test.go (about)

     1  package util
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"testing"
     7  )
     8  
     9  func TestURLClient_HTTPDo(t *testing.T) {
    10  	client, err := GetURLClient(nil)
    11  	if err != nil {
    12  		t.Errorf("GetURLClient error: %v", err)
    13  	}
    14  
    15  	ts := getMockServer(t)
    16  	resp, err := client.HTTPDo("GET", ts.URL+"/test", http.Header{}, nil)
    17  	if err != nil {
    18  		t.Errorf("HTTPDo error: %v", err)
    19  	}
    20  
    21  	if resp.StatusCode != http.StatusOK {
    22  		t.Errorf("got error status code, resp is %v", resp)
    23  	}
    24  }
    25  
    26  func getMockServer(t *testing.T) *httptest.Server {
    27  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    28  		switch r.Method {
    29  		case "GET":
    30  			if r.URL.EscapedPath() != "/test" {
    31  				t.Errorf("path error: %s", r.URL.EscapedPath())
    32  				w.WriteHeader(http.StatusNotFound)
    33  			} else {
    34  				w.WriteHeader(http.StatusOK)
    35  			}
    36  		}
    37  	}))
    38  
    39  	return ts
    40  }