<commit_before>package vm

import (
	"testing"
	//"net/http/httptest"
	//"net/http"
	"fmt"
	"io/ioutil"
	"net/http"
)

//chan parameter for blocking until server is prepared
func startTestServer(c chan bool) {
	m := http.NewServeMux()

	m.HandleFunc("/index", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)

		if r.Method == http.MethodPost {
			b, err := ioutil.ReadAll(r.Body)
			if err != nil {
				panic(err)
			}
			fmt.Fprintf(w, "POST %s", b)
		} else {
			fmt.Fprint(w, "GET Hello World")
		}

	})

	m.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(404)
		fmt.Fprint(w, "oops")
	})

	m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Println(r.URL.Path)
	})

	c <- true

	http.ListenAndServe(":3000", m)
}

func TestHTTPObject(t *testing.T) {

	//blocking channel
	c := make(chan bool, 1)

	//server to test off of
	go startTestServer(c)

	tests := []struct {
		input    string
		expected interface{}
	}{
		//test get request
		{`
		require "net/http"

		Net::HTTP.get("http://127.0.0.1:3000/index")
		`, "GET Hello World"},
		{`
		require "net/http"

		Net::HTTP.post("http://127.0.0.1:3000/index", "text/plain", "Hi Again")
		`, "POST Hi Again"},
	}

	//block until server is ready
	<-c

	for i, tt := range tests {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkExpected(t, i, evaluated, tt.expected)
		v.checkCFP(t, i, 0)
		v.checkSP(t, i, 1)
	}
}

func TestHTTPObjectFail(t *testing.T) {
	//blocking channel
	c := make(chan bool, 1)

	//server to test off of
	go startTestServer(c)

	testsFail := []errorTestCase{
		{`
		require "net/http"

		Net::HTTP.get("http://127.0.0.1:3000/error")
		`, "InternalError: 404 Not Found", 4},
		{`
		require "net/http"

		Net::HTTP.post("http://127.0.0.1:3000/error", "text/plain", "Let me down")
		`, "InternalError: 404 Not Found", 4},
	}

	//block until server is ready
	<-c

	for i, tt := range testsFail {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkError(t, i, evaluated, tt.expected, getFilename(), tt.errorLine)
		v.checkCFP(t, i, 1)
		v.checkSP(t, i, 1)
	}
}<commit_msg>added test for connection refused error<commit_after>package vm

import (
	"testing"
	//"net/http/httptest"
	//"net/http"
	"fmt"
	"io/ioutil"
	"net/http"
)

//chan parameter for blocking until server is prepared
func startTestServer(c chan bool) {
	m := http.NewServeMux()

	m.HandleFunc("/index", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)

		if r.Method == http.MethodPost {
			b, err := ioutil.ReadAll(r.Body)
			if err != nil {
				panic(err)
			}
			fmt.Fprintf(w, "POST %s", b)
		} else {
			fmt.Fprint(w, "GET Hello World")
		}

	})

	m.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(404)
		fmt.Fprint(w, "oops")
	})

	m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Println(r.URL.Path)
	})

	c <- true

	http.ListenAndServe(":3000", m)
}

func TestHTTPObject(t *testing.T) {

	//blocking channel
	c := make(chan bool, 1)

	//server to test off of
	go startTestServer(c)

	tests := []struct {
		input    string
		expected interface{}
	}{
		//test get request
		{`
		require "net/http"

		Net::HTTP.get("http://127.0.0.1:3000/index")
		`, "GET Hello World"},
		{`
		require "net/http"

		Net::HTTP.post("http://127.0.0.1:3000/index", "text/plain", "Hi Again")
		`, "POST Hi Again"},
	}

	//block until server is ready
	<-c

	for i, tt := range tests {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkExpected(t, i, evaluated, tt.expected)
		v.checkCFP(t, i, 0)
		v.checkSP(t, i, 1)
	}
}

func TestHTTPObjectFail(t *testing.T) {
	//blocking channel
	c := make(chan bool, 1)

	//server to test off of
	go startTestServer(c)

	testsFail := []errorTestCase{
		{`
		require "net/http"

		Net::HTTP.get("http://127.0.0.1:3000/error")
		`, "InternalError: 404 Not Found", 4},
		{`
		require "net/http"

		Net::HTTP.post("http://127.0.0.1:3000/error", "text/plain", "Let me down")
		`, "InternalError: 404 Not Found", 4},
		{`
		require "net/http"

		Net::HTTP.post("http://127.0.0.1:3001", "text/plain", "Let me down")
		`, "InternalError: Post http://127.0.0.1:3001: dial tcp 127.0.0.1:3001: getsockopt: connection refused", 4},
	}

	//block until server is ready
	<-c

	for i, tt := range testsFail {
		v := initTestVM()
		evaluated := v.testEval(t, tt.input, getFilename())
		checkError(t, i, evaluated, tt.expected, getFilename(), tt.errorLine)
		v.checkCFP(t, i, 1)
		v.checkSP(t, i, 1)
	}
}
{'fraction_non_alphanumeric': 0.1843417913684667, 'fraction_numerical': 0.03318152244632401, 'mean_word_length': 3.342749529190207, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 11, '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': '6225227', 'n_tokens_mistral': 2061, 'n_tokens_neox': 1812, 'n_words': 422}