brickklion.blogg.se

Go json compare test
Go json compare test












go json compare test

I hope this is useful and will help you to write better code.

Go json compare test how to#

Thanks to its httptest package your tests can spawn a simple HTTP server which you can use to inspect these requests. We will learn how to convert from JSON raw data (strings or bytes) into Go types like structs, arrays, and slices, as well as unstructured data like maps and empty interfaces. ConclusionĪs you’ve seen, it’s very easy to test that your code is making the correct HTTP requests in Go. With this in place, Publish() will now make a POST request to the right URL and with the right body.

go json compare test

NOTE: In this simple example I’m just using fmt.Sprintf() to build the request JSON, but if the JSON is more complex I would prefer to use simplejson. You want to test that the format of the POSTed data is as expected.Return an error if the response is not acceptable.You need to publish an NSQ message in your application-specific format: Publish(nsqdUrl, msg string) error I’ll walk you through the following example.

go json compare test

I tried to keep this example as toy as possible without making it useless.ĭon’t worry too much about the exact details about the specific JSON format, URL, etc…what’s important is that your code is making an HTTP request and you want to test it. What do you test and how? I’m sure you run into something similar. Let’s say you have some code that make an HTTP request with some JSON data. You could argue a simpler initial step would be just to assert that the response body has a particular JSON string. Go Testing Technique: Testing JSON HTTP Requests














Go json compare test