<commit_before>package restful

import (
	"log"
	"net/http"
	"net/http/httptest"
	"net/url"
	"strings"
	"testing"
	"time"
)

type testData struct {
	subject  string
	url      string
	method   string
	argument map[string]string
	expected string
}

var (
	rtr           = BuildCellAdvisorRestfulAPI(4, "10.82.26.12", time.Second*10)
	testDataArray = []testData{
		testData{
			subject:  "touch : Missing y value",
			url:      "/api/scpi/touch",
			method:   "POST",
			argument: map[string]string{"x": "10"},
			expected: "value missing",
		},
		testData{
			subject:  "touch : X, Y Exist",
			url:      "/api/scpi/touch",
			method:   "POST",
			argument: map[string]string{"x": "10", "y": "20"},
			expected: "OK",
		},
		testData{
			subject:  "keyp : value given",
			url:      "/api/scpi/keyp",
			method:   "POST",
			argument: map[string]string{"value": "MODE"},
			expected: "OK",
		},
		testData{
			subject:  "keyp : value not given",
			url:      "/api/scpi/keyp",
			method:   "POST",
			argument: map[string]string{},
			expected: "keyp value missing",
		},
		testData{
			subject:  "refresh_screen : ",
			url:      "/api/screen/refresh_screen",
			method:   "POST",
			argument: map[string]string{},
			expected: "OK",
		},
		testData{
			subject:  "screen : ",
			url:      "/api/screen/screen",
			method:   "GET",
			argument: map[string]string{},
			expected: "JFIF",
		},
		testData{
			subject:  "unknown command : ",
			url:      "/api/scpi/heyoman",
			method:   "POST",
			argument: map[string]string{},
			expected: "unknown",
		},
	}
)

func createQuery(argument map[string]string) url.Values {
	u := url.Values{}
	for k, v := range argument {
		u.Set(k, v)
	}
	return u
}
func TestSCPIArgument(t *testing.T) {

	var v url.Values
	var r *http.Request
	var err error
	for _, testcase := range testDataArray {
		v = createQuery(testcase.argument)
		if testcase.method == "POST" {
			r, err = http.NewRequest(testcase.method, testcase.url, strings.NewReader(v.Encode()))
			r.Form = v
		} else {
			r, err = http.NewRequest(testcase.method, testcase.url, nil)
		}
		if err != nil {
			t.Log(err.Error())
			return
		}

		w := httptest.NewRecorder()
		rtr.ServeHTTP(w, r)
		log.Println(w.Code)
		if b := w.Body.String(); !strings.Contains(b, testcase.expected) {
			t.Logf("inner testcases : %s", testcase.subject)
			log.Println(w.Code)
			t.Fatalf("body = %s, want %s", b, testcase.expected)
		}
	}

}<commit_msg>Status Code checked<commit_after>package restful

import (
	"net/http"
	"net/http/httptest"
	"net/url"
	"strings"
	"testing"
	"time"
)

type testData struct {
	subject      string
	url          string
	method       string
	argument     map[string]string
	expected     string
	expectedCode int
}

var (
	rtr           = BuildCellAdvisorRestfulAPI(4, "10.82.26.12", time.Second*10)
	testDataArray = []testData{
		testData{
			subject:      "touch : Missing y value",
			url:          "/api/scpi/touch",
			method:       "POST",
			argument:     map[string]string{"x": "10"},
			expected:     "value missing",
			expectedCode: http.StatusBadRequest,
		},
		testData{
			subject:      "touch : X, Y Exist",
			url:          "/api/scpi/touch",
			method:       "POST",
			argument:     map[string]string{"x": "10", "y": "20"},
			expected:     "OK",
			expectedCode: http.StatusOK,
		},
		testData{
			subject:      "keyp : value given",
			url:          "/api/scpi/keyp",
			method:       "POST",
			argument:     map[string]string{"value": "MODE"},
			expected:     "OK",
			expectedCode: http.StatusOK,
		},
		testData{
			subject:      "keyp : value not given",
			url:          "/api/scpi/keyp",
			method:       "POST",
			argument:     map[string]string{},
			expected:     "keyp value missing",
			expectedCode: http.StatusBadRequest,
		},
		testData{
			subject:      "refresh_screen : ",
			url:          "/api/screen/refresh_screen",
			method:       "POST",
			argument:     map[string]string{},
			expected:     "OK",
			expectedCode: http.StatusOK,
		},
		testData{
			subject:      "screen : ",
			url:          "/api/screen/screen",
			method:       "GET",
			argument:     map[string]string{},
			expected:     "JFIF",
			expectedCode: http.StatusOK,
		},
		testData{
			subject:      "unknown command : ",
			url:          "/api/scpi/heyoman",
			method:       "POST",
			argument:     map[string]string{},
			expected:     "unknown",
			expectedCode: http.StatusBadRequest,
		},
	}
)

func createQuery(argument map[string]string) url.Values {
	u := url.Values{}
	for k, v := range argument {
		u.Set(k, v)
	}
	return u
}
func TestSCPIArgument(t *testing.T) {

	var v url.Values
	var r *http.Request
	var err error
	for _, testcase := range testDataArray {
		v = createQuery(testcase.argument)
		if testcase.method == "POST" {
			r, err = http.NewRequest(testcase.method, testcase.url, strings.NewReader(v.Encode()))
			r.Form = v
		} else {
			r, err = http.NewRequest(testcase.method, testcase.url, nil)
		}
		if err != nil {
			t.Log(err.Error())
			return
		}

		w := httptest.NewRecorder()
		rtr.ServeHTTP(w, r)
		if b := w.Body.String(); !strings.Contains(b, testcase.expected) {
			t.Logf("inner testcases : %s", testcase.subject)
			t.Fatalf("body = %s, want %s", b, testcase.expected)
		}
		if w.Code != testcase.expectedCode {
			t.Logf("inner testcases : %s", testcase.subject)
			t.Fatalf("code = %d, want %d", w.Code, testcase.expectedCode)
		}
	}

}
{'fraction_non_alphanumeric': 0.15798226164079823, 'fraction_numerical': 0.006282335550628234, 'mean_word_length': 2.759027777777778, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5029717', 'n_tokens_mistral': 2229, 'n_tokens_neox': 1963, 'n_words': 432}