Skip to content

A library that facilitates the creation of a fake server for testing and development purposes.

Notifications You must be signed in to change notification settings

rebecaalbuquerque/android-fake-server

Repository files navigation

Fake Server

Imagine during the development of your app being able to configure the response of your endpoints quickly, without having to worry about setting up software like Mockoon or Flipper.

Fake Server is a library for Android development that facilitates creating a fake server for testing and development purposes. It allows you to register fake responses for different URLs and return these responses during testing or development.

Installation

Add the JitPack repository to your build.gradle (at the project level):

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Add the dependency to your build.gradle (at the module level):

dependencies {
  implementation 'com.github.rebecaalbuquerque:android-fake-server:$version'
}

Usage

Initialize FakeServer

Preferably, initialize it in the Application for a global configuration.

class MyApplication: Application() {

    override fun onCreate() {
        super.onCreate()
        FakeServer.initialize(this)
    }
}

(Optional) Register a fake response:

FakeServer.registerResponse(
  endpoint = "path-to-endpoint",
  response = """
      {
          "id": 1,
          "name": "test"
      }
  """
)

Create your service layer

interface MyApiService {

  @GET("fake-server/lorem-ipsum")
  fun getFakeLoremIpsum(
      @Header(FakeServer.HEADER_DELAY) delay: Int? = null,
      @Header(FakeServer.HEADER_STATUS_CODE) statusCode: String? = null
  ): Single<ResponseBody>

  @GET("lorem-ipsum")
  fun getLoremIpsum(): Single<ResponseBody>
}

Set up the Activity/Fragment

To use Fake Server, you need to add the FakeServerInterceptor to your OkHttp client. delay and statusCode are optional.

class MainActivity : AppCompatActivity() {

  private val apiService: MyApiService by lazy {
      val client = OkHttpClient.Builder()
        .addInterceptor(FakeServerInterceptor())
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .build()
  
      val retrofit = Retrofit.Builder()
          .baseUrl("https://your-base-url/")
          .cliet(client)
          .addConverterFactory(GsonConverterFactory.create())
          .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
          .build()

      retrofit.create(MyApiService::class.java)
  }

  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)

      binding = ActivityMainBinding.inflate(layoutInflater)
      setContentView(binding?.root)

      fetchFakeData()
      fetchData()
  }

  private fun fetchFakeData() {
      apiService.getFakeLoremIpsum(delay = 2500, statusCode = 123)
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
          .subscribe({ response ->
              // Handle the response
          }, { throwable ->
              // Handle the failure
          })
  }

  private fun fetchData() {
      apiService.getLoremIpsum()
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
          .subscribe({ response ->
              // Handle the response
          }, { throwable ->
              // Handle the failure
          })
  } 
}

Screenshots/GIF

About

A library that facilitates the creation of a fake server for testing and development purposes.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages