// NewDummyUploader creates a new TCP net.Listener at a random port, a new http.ServerMux responding
// upload routes and a new http.Server with the new mux. This function also creates
// a go routine that calls server.Serve(listener).
NewDummyUploader creates a new TCP net.Listener at a random port, a new http.ServerMux responding
upload routes and a new http.Server with the new mux. This function also creates
a go routine that calls server.Serve(listener).
// NewDummyUploader creates a new TCP net . Listener at a random port a new http . ServerMux responding // upload routes and a new http . Server with the new mux . This function also creates // a go routine that calls server . Serve ( listener ) .
func (f *HTTPServerFactory) NewDummyUploader() (net.Listener, *http.Server, error) {
	listener, err := net.Listen("tcp", ":0")
	if err != nil {
		return nil, nil, err
	}
	mux := http.NewServeMux()
	server := &http.Server{
		Handler: mux,
	}
	mux.HandleFunc("/upload-info", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodGet {
			w.WriteHeader(http.StatusMethodNotAllowed)
			return
		}
		port := listener.Addr().(*net.TCPAddr).Port
		uploadInfo := &struct {
			Method, URL string
			Headers     http.Header
		}{
			Method: http.MethodPut,
			URL:    fmt.Sprintf("http://localhost:%d/upload", port),
			Headers: http.Header{
				"Content-Length": []string{r.Header.Get(file.FileUploadSizeHeader)},
			},
		}
		payload, err := json.Marshal(uploadInfo)
		if err != nil {
			panic(fmt.Errorf("error marshaling dummy upload HTTP server response: %w", err))
		}
		if _, err := w.Write(payload); err != nil {
			panic(fmt.Errorf("error writing dummy upload HTTP server response: %w", err))
		}
	})
	var uploadedData []byte
	mux.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
		if r.Method == http.MethodPut {
			var err error
			uploadedData, err = ioutil.ReadAll(r.Body)
			if err != nil {
				panic(fmt.Errorf("error reading upload: %w", err))
			}
			w.WriteHeader(http.StatusOK)
		} else if r.Method == http.MethodGet {
			if _, err := w.Write(uploadedData); err != nil {
				panic(fmt.Errorf("error writing upload: %w", err))
			}
		} else {
			w.WriteHeader(http.StatusMethodNotAllowed)
		}
	})
	go f.serveAndPanicOnError(server, listener, "dummy uploader")
	return listener, server, nil
}
func ( f * HTTPServerFactory ) NewDummyUploader ( ) ( net . Listener , * http . Server , error ) { listener , err := net . Listen ( " " , " " ) if err != nil { return nil , nil , err } mux := http . NewServeMux ( ) server := & http . Server { Handler : mux , } mux . HandleFunc ( " " , func ( w http . ResponseWriter , r * http . Request ) { if r . Method != http . MethodGet { w . WriteHeader ( http . StatusMethodNotAllowed ) return } port := listener . Addr ( ) . ( * net . TCPAddr ) . Port uploadInfo := & struct { Method , URL string Headers http . Header } { Method : http . MethodPut , URL : fmt . Sprintf ( " " , port ) , Headers : http . Header { " " : [ ] string { r . Header . Get ( file . FileUploadSizeHeader ) } , } , } payload , err := json . Marshal ( uploadInfo ) if err != nil { panic ( fmt . Errorf ( " " , err ) ) } if _ , err := w . Write ( payload ) ; err != nil { panic ( fmt . Errorf ( " " , err ) ) } } ) var uploadedData [ ] byte mux . HandleFunc ( " " , func ( w http . ResponseWriter , r * http . Request ) { if r . Method == http . MethodPut { var err error uploadedData , err = ioutil . ReadAll ( r . Body ) if err != nil { panic ( fmt . Errorf ( " " , err ) ) } w . WriteHeader ( http . StatusOK ) } else if r . Method == http . MethodGet { if _ , err := w . Write ( uploadedData ) ; err != nil { panic ( fmt . Errorf ( " " , err ) ) } } else { w . WriteHeader ( http . StatusMethodNotAllowed ) } } ) go f . serveAndPanicOnError ( server , listener , " " ) return listener , server , nil }
NewDummyUploader creates a new TCP net.Listener at a random port, a new http.ServerMux responding
upload routes and a new http.Server with the new mux.
NewDummyUploader creates a new TCP net . Listener at a random port a new http . ServerMux responding upload routes and a new http . Server with the new mux .
// create listener at a random port // create server and mux