Ejemplo n.º 1
0
func TestServices_Each(t *testing.T) {
	services := httpservice.Services{
		"service1": {
			Weight: 5,
		},
		"service2": {
			Weight: -1,
		},
		"service3": {
			Weight: 2,
		},
		"service4": {
			Weight: 1,
		},
		"service5": {
			Weight: 0,
		},
		"service6": {
			Weight: 4,
		},
	}

	i := 0
	expected := []int{-1, 0, 1, 2, 4, 5}
	for service := range services.Each() {
		if want, have := expected[i], service.Weight; want != have {
			t.Errorf("[%d] exptected %d, got %d", i, want, have)
		}
		i++
	}
}
Ejemplo n.º 2
0
func TestServices_Patch(t *testing.T) {
	resultKey := "result"
	servicePath := "/foo/bar"

	// dummy service to test
	var services httpservice.Services = make(map[string]*httpservice.Service)
	s, mware := testServiceSuit(servicePath, resultKey)
	services["example"] = s

	m := http.NewServeMux()

	// lazy implementation routerfunc for ServeMux
	// that simply skip method handling (don't try this at home)
	rtfn := func(m *http.ServeMux) httpservice.RouterFunc {
		return func(path string, methods []string, h http.Handler) error {
			m.Handle(path, h)
			return nil
		}
	}(m)

	// add middleware with patch
	patch := func(services httpservice.Services) httpservice.Services {
		for name := range services {
			services[name].Middlewares.Add(httpservice.MWInner, mware)
		}
		return services
	}

	// route dumy service
	services.Patch(patch)
	services.Route(rtfn)

	// variables for decoding
	w := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", servicePath, nil)
	m.ServeHTTP(w, req)
	vmap := make(map[string]interface{})

	// try decoding
	dec := json.NewDecoder(w.Body)
	if err := dec.Decode(&vmap); err != nil {
		t.Errorf("error decoding response: %#v", err.Error())
	} else if result, ok := vmap[resultKey]; !ok {
		t.Errorf("got no %#v in vmap: %#v", resultKey, vmap)
	} else if want, have := "hello world", result; want != have {
		t.Errorf("expect: %#v, got: %#v", want, have)
	}
}