How to write Unit tests for http requests(Go)?

Hi all, learn to write unit tests, I can't really understand how to write test for http requests.
Suppose there is a function that takes XML(yml) file from a network url.
func (y *Yml) LoadXMLFromURL(url string) error {
 response, err := http.Get(url)
 if err != nil {
 return err
}
 answer, err := ioutil.ReadAll(response.Body)
 if err != nil {
 return err
}
 err = xml.Unmarshal(answer, y)
 return err
}


And I can't quite understand how to test this function, use the XML from the network, if the url from this XML is not available, the test will fail.
I thought you can just write the XML into a variable and use it, is it true I think?
July 2nd 19 at 17:54
2 answers
July 2nd 19 at 17:56
Solution
Thank you - Lorine86 commented on July 2nd 19 at 18:02
July 2nd 19 at 17:58
If you don't know how to test the feature is a bad feature)

You have the condition - returns nil if no network no resource no error in the response or invalid XML. So it is necessary to reproduce all of the situations in the tests, ideally in every situation one method. It is easier to test the latest situation, picking up with localhost invalid XML returned not nil test fell.
In what sense is a bad feature? I don't really know how you can write a function to receive XML, so that you can protest a http request. Here it is not the function, but the fact of the request, as you can test that depends on external factors? - barney.Schroeder90 commented on July 2nd 19 at 18:01
: so the http requests don't test for unit tests. You have here, in principle, to test there is nothing, just spread the results from one library function to another. - Lorine86 commented on July 2nd 19 at 18:04

Find more questions by tags Modular testingTesting softwareGo