"go custom http request testing" Code Answer's

You're definitely familiar with the best coding language Go that developers use to develop their projects and they get all their queries like "go custom http request testing" answered properly. Developers are finding an appropriate answer about go custom http request testing related to the Go coding language. By visiting this online portal developers get answers concerning Go codes question like go custom http request testing. Enter your desired code related query in the search bar and get every piece of information about Go code related question on go custom http request testing. 

go custom http request testing

By Restu Wahyu SaputraRestu Wahyu Saputra on Apr 01, 2021
package main

import (
	"bytes"
	"encoding/binary"
	"net/http"
	"net/http/httptest"

	"github.com/sirupsen/logrus"
)

// helper.go
func JSONStrigify(payload string) []byte {
	jsonData := []byte(payload)
	return jsonData
}

// request.go
func HttpTestRequest(handler http.Handler, method, url string, payload []byte) *httptest.ResponseRecorder {

	request := make(chan *http.Request)
	errors := make(chan error)

	if binary.Size(payload) > 0 {
		req, err := http.NewRequest(method, url, bytes.NewBuffer(payload))
		go func() {
			request <- req
			errors <- err
		}()
	} else {
		req, err := http.NewRequest(method, url, nil)
		go func() {
			request <- req
			errors <- err
		}()
	}

	if <-errors != nil {
		logrus.Error(<-errors)
	}

	rr := httptest.NewRecorder()
	handler.ServeHTTP(rr, <-request)

	return rr
}

// main_test.go
func TestCreateAccount(t *testing.T) {

	router := setupRouter()

	res := helper.HttpTestRequest(router, "POST", "/account", helper.JSONStrigify(`{
		"name":"xyz",
		"ic_type":"pqr",
		"ic_number_identity":"[email protected]",
		"account_number":"1234567890",
		"account_name":"1234567890",
		"bank_name":"1234567890",
		"type":"1234567890"
	}`))

	assert.Equal(t, http.StatusOK, res.Code)
}

Add Comment

0

All those coders who are working on the Go based application and are stuck on go custom http request testing can get a collection of related answers to their query. Programmers need to enter their query on go custom http request testing related to Go code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about go custom http request testing for the programmers working on Go code while coding their module. Coders are also allowed to rectify already present answers of go custom http request testing while working on the Go language code. Developers can add up suggestions if they deem fit any other answer relating to "go custom http request testing". Visit this developer's friendly online web community, CodeProZone, and get your queries like go custom http request testing resolved professionally and stay updated to the latest Go updates. 

Go answers related to "go custom http request testing"

View All Go queries

Go queries related to "go custom http request testing"

Browse Other Code Languages

CodeProZone