Thursday, June 9, 2016

Test Coverage In Go

In my previous post I showed how to write basic tests in Golang. In this post we will go through how to identify test coverage in Golang with it's inbuilt commands. I will continue with the source code from previous post.
First run following command. Output is about basic details of test coverage in our current code base.
Next add new function like below.
Again run command for test coverage. This time output is different.
If you need to get function wise output, then we need to run following command.
Further if you need to export those data into html, you can use following command. It will output the details into your default web browser.
The output is like below.

Testing in Golang

Golang is one of the programming language which use convention over configuration. In Go application, we can write a test file by adding Test suffix to test source file. Then this file will be ignored in Go build process. And also test runner can inspect test functions in these files and execute tests.
There are three types of tests in Go.

  • Basic Tests (Regular tests)
Basic tests are for identify application is performing accurately. These tests required to have Test prefix in test functions.
  • Benchmark Tests

Benchmark test are for identify performance with related to limits. These tests required to have Benchmark prefix in functions.

  • Example Tests
Example tests are integrated with GoDoc tool. These test required to have Example as prefix.
In this post, we will create a Basic test. We are going to do Test Driven Development, therefore we need to first write test functions.
Then write the actual code.
Now using following command we can run test runner. The output will be like below.
Next will change result comparing value for demonstration purpose. Then our test function become incorrect.
Run go test command again. Then output is like below.

Saturday, June 4, 2016

OAuth2 with Golang

In my previous post I showed how to have an authentication handler. In this post I will continue with that source and implement user login using OAuth2. I will use Google as the provider.
Gomniauth is a better solution as GoLang OAuth2 package. First we need to install Gomniauth package.
In this demo we will use Google as the authentication provider. Using Google Developer Console you can create a client id and client secrets. And also you have to give host URL and re directional URL. Then will setup Gomniauth in main.go file.
Next will add login.html file.
Add endpoint for login.html in main function.
In login.html Google login link is given as "/auth/login/google". We need to write a login handler which can identify the provider and redirect request accordingly. Because in future we can add more providers like Facebook, Github and etc.
Next we need to write callback handler. In callback, we can retrieve user's basic data using Gomniauth functions. Once we have response data, we Base64 encode full name and save it into a cookie called auth cookie. Then we can retrieve those details later from the cookie and show it in our web pages.
Update templateHandler function like below.
Update home.html file to show name retrieved from Google login.

Friday, June 3, 2016

Authentication Wrapper Handler Using Go

In this post I will show how to secure your routes by wrapping http handler.
First add main.go file with following content.
Add a folder called templates and add home.html file with following contents.
Then build and run the application.
Output should be displayed like below.
Now we need to restrict access for /home route if "auth" cookie is not present. Add auth.go file which will contains all the authentication related functionalities.
Now we need to change the main function accordingly.
Build and run the application. You will see 404 page like below.