コード例 #1
0
func TestServiceWrapper(t *testing.T) {
	backend := expander.NewExpander("../expansion/expansion.py")
	wrapper := NewService(NewExpansionHandler(backend))
	container := restful.NewContainer()
	container.ServeMux = http.NewServeMux()
	wrapper.Register(container)
	handlerTester := util.NewHandlerTester(container)
	for _, swtc := range ServiceWrapperTestCases {
		reader := GetTemplateReader(t, swtc.Description, inputFileName)
		w, err := handlerTester(swtc.HTTPMethod, swtc.ServiceURLPath, swtc.ContentType, reader)
		if err != nil {
			t.Errorf("error in test case '%s': %s\n", swtc.Description, err)
		}

		if w.Code != http.StatusOK {
			if w.Code != swtc.StatusCode {
				message := fmt.Sprintf("test returned code:%d, status: %s", w.Code, w.Body.String())
				t.Errorf("error in test case '%s': %s\n", swtc.Description, message)
			}
		} else {
			if swtc.StatusCode != http.StatusOK {
				t.Errorf("expected error did not occur in test case '%s': want: %d have: %d\n",
					swtc.Description, swtc.StatusCode, w.Code)
			}

			body := w.Body.Bytes()
			actualResponse := &expander.ExpansionResponse{}
			if err := json.Unmarshal(body, actualResponse); err != nil {
				t.Errorf("error in test case '%s': %s\n", swtc.Description, err)
			}

			actualResult, err := actualResponse.Unmarshal()
			if err != nil {
				t.Errorf("error in test case '%s': %s\n", swtc.Description, err)
			}

			expectedOutput := GetOutputString(t, swtc.Description)
			expectedResult := expandOutputOrDie(t, expectedOutput, swtc.Description)

			if !reflect.DeepEqual(expectedResult, actualResult) {
				message := fmt.Sprintf("want: %s\nhave: %s\n",
					util.ToYAMLOrError(expectedResult), util.ToYAMLOrError(actualResult))
				t.Errorf("error in test case '%s':\n%s\n", swtc.Description, message)
			}
		}
	}
}
コード例 #2
0
func TestExpansionHandler(t *testing.T) {
	backend := &mockExpander{}
	wrapper := NewService(NewExpansionHandler(backend))
	container := restful.DefaultContainer
	wrapper.Register(container)
	handlerTester := util.NewHandlerTester(container)
	for _, ehtc := range ExpansionHandlerTestCases {
		reader := GetTemplateReader(t, ehtc.Description, ehtc.TemplateFileName)
		w, err := handlerTester(httpPOSTMethod, validServiceURL, jsonContentType, reader)
		if err != nil {
			t.Errorf("error in test case '%s': %s\n", ehtc.Description, err)
		}

		if w.Code != http.StatusBadRequest {
			t.Errorf("expected error did not occur in test case '%s': want: %d have: %d\n",
				ehtc.Description, http.StatusBadRequest, w.Code)
		}
	}
}