🚀 Get 25% off access to all my premium courses - use discount code FUNCMAIN at checkout - view the pricing page now!

Video:

Migrating Our Database and Testing our Endpoints Manually

December 28, 2021

Course Instructor: Elliot Forbes

Hey Gophers! My name is Elliot and I'm the creator of TutorialEdge and I've been working with Go systems for roughly 5 years now.

Twitter: @Elliot_f

Testing Manually with curl

In hindsight, when recording this video I assumed viewers would know how to work with Postman which was a mistake. If you aren’t familiar with Postman then this section will hopefully highlight an alternative approach to testing your endpoint manually using curl:

Starting our Application

Once we have added the migration code, let’s try starting up our application locally:

$ go run cmd/server/main.go
Setting Up Our APP
Setting up new database connection
Setting Up Routes

Perfect, our application is now up and running and ready to go!

Testing Get All Comments Endpoint

When we first start up our application, at this point there should be nothing within our database. Let’s test this theory out by hitting the get all comments Endpoint with a curl command:

$ curl http://localhost:8080/api/comment
[]

Now the above command will send a HTTP GET request to http://localhost:8080/api/comment which is the correct port that our application is currently running with locally.

Posting a New Comment

Let’s now try and create a new comment by hitting the HTTP POST /api/comment endpoint using curl again:

$ curl -X POST http://localhost:8080/api/comment
{Model:{ID: 1 CreatedAt: 2021-02-13 12:38:43.405483 +0000 GMT m=+97.914815763 UpdatedAt 2021-02-13 12:38:43.405483 +0000 GMT m=+97.914815763 DeletedAt:<nil>} Slug:/ Body: Author:}

Now at this point, we aren’t returning proper JSON-formatted string at this point, we are just returning the string representation of the struct that we have created within our endpoint.

Testing Persistence

Now that we have inserted something into the database, let’s try hitting the all comments endpoint again:

$ curl http://localhost:8080/api/comment
[{Model:{ID: 1 CreatedAt: 2021-02-13 12:38:43.405483 +0000 GMT m=+97.914815763 UpdatedAt 2021-02-13 12:38:43.405483 +0000 GMT m=+97.914815763 DeletedAt:<nil>} Slug:/ Body: Author:}]

Perfect, our application has been able to fetch this newly created comment from the database and return it to us.

Updating a Comment with PUT

At this point, we’ve hardcoded the update functionality within our app to only update the comment with ID 1. Hitting this endpoint will update this comment and set the Slug to /new. In future videos, we’ll look at how we can parse the incoming body of a request and use that to update a comment properly, but for now this allows us to quickly validate that our routes are working and that the database is working as intended.

$ curl -X PUT http://localhost:8080/api/comment/1
{Model:{ID: 1 CreatedAt: 2021-02-13 12:38:43.405483 +0000 GMT m=+97.914815763 UpdatedAt 2021-02-13 12:38:43.405483 +0000 GMT m=+97.914815763 DeletedAt:<nil>} Slug:/new Body: Author:}

Perfect, you can see that we’ve been able to update the comment in the database and it now has the value /new within the Slug field.

Conclusion

Perfect, we’ve been able to test the majority of the endpoints that we have defined using both Postman within the video and curl if you are following along the text version.

If you’d like more information on sending curl requests then I recommend these 2 articles below: